I am getting an Index in position 1 exceeds array bounds error

조회 수: 417 (최근 30일)
William Sheehan
William Sheehan 2018년 12월 19일
댓글: Walter Roberson 2023년 5월 6일
I am getting the following index error:
Index in position 1 exceeds array bounds (must not exceed 1).
Error in L_Individual_Correlation_With_Players>DispCorr (line 36)
MeanSI1 = nanmean(RSI1(1:22,:),1);
Error in L_Individual_Correlation_With_Players (line 8)
IndPhasesDispCorr = DispCorr(IndGameDispersion,PhasesTimeStamps,PhasesDuration)
I don't understand why this error is occuring. If I run a few sample points individually, the error doesn't occur. Any help would be greatly appreciated. I have attached the sample workspace.
Here is the script:
IndPhasesDispCorr = DispCorr(IndGameDispersion,PhasesTimeStamps,PhasesDuration);
function IndDispCorr = DispCorr(IndGameDispersion,Idx,Context)
IndDispCorr = zeros(size(length(Idx)));
count = 0;
for k = 1:length(Idx)
PhaseWindow = Idx(k)+(Context(k,1)*10);
if PhaseWindow < Idx(k)
t = PhaseWindow:Idx(k);
else
t = Idx(k):PhaseWindow;
end
M = IndGameDispersion(t,:);
RSI1 = corrcoef([M(:,1:22) nanmean(M(:,1:22),2)],'rows','pairwise');
Rx1 = corrcoef([M(:,23:44) nanmean(M(:,23:44),2)],'rows','pairwise'); %Last column is centroid coordinates
Ry1 = corrcoef([M(:,45:66) nanmean(M(:,23:44),2)],'rows','pairwise');
RSI1(RSI1 == 1) = NaN;
Rx1(Rx1 == 1) = NaN;
Ry1(Ry1 == 1) = NaN;
MeanSI1 = nanmean(RSI1(1:22,:),1);
MeanRx1 = nanmean(Rx1(1:22,:),1);
MeanRy1 = nanmean(Ry1(1:22,:),1);
CorrN = [MeanSI1,nanmean(MeanSI1(1:22),2),MeanRx1,nanmean(MeanRx1(1:22),2),MeanRy1,nanmean(MeanRy1(1:22),2)];
count = count + 1;
IndDispCorr(count,1:72) = CorrN;
end
end
  댓글 수: 8
Walter Roberson
Walter Roberson 2022년 2월 26일
Why do you assume that the uitable has at least 20 rows?
Image Analyst
Image Analyst 2022년 2월 26일
for i = 1 : height(app.UITable.Data)
E(i) = app.UITable.Data{i, 1}

댓글을 달려면 로그인하십시오.

답변 (3개)

Cris LaPierre
Cris LaPierre 2018년 12월 19일
편집: Cris LaPierre 2018년 12월 19일
The error means you are trying to index into an array, but are using indices that exceed the size of the array.
These are errors you can investigate using MATLAB debugging tools.
The simple answer is that when k=295, RSI1 is a 1x1 with value NaN. The line of code giving the error is asking for rows 1:22, which don't exist.
There is a progression of issues here. Basically, when k==295, (Context(k,1)==0. The result is PhaseWindow == Idx(k), so t=PhaseWindow = 4129.
It looks like you wrote the code assuming Context(k,1) would always be greater than 0. You just need to add code to handle this scenario and you should be good.

Image Analyst
Image Analyst 2018년 12월 19일
When you do this:
MeanSI1 = nanmean(RSI1(1:22,:),1);
It's saying that RSI1 has only 1 row, not 22.
  댓글 수: 2
Apica Sharma
Apica Sharma 2021년 11월 14일
HOw to solve this ?
Image Analyst
Image Analyst 2021년 11월 14일
Nope, not without diving into it more. Why did you pick 22 in the first place? Are you certain that there should be exactly 22 rows and not just 1 or 10 or some other variable number of rows? If you want to average the entire array (all rows and columns) regardless of how many rows and columns there are, and get the mean value for each column, then you can do this
% Average down along (within) columns row-by-row (direction 1).
% In other words, get a mean for each column.
columnMeansSI1 = mean(RSI1, 1, 'omitnan');
% Average across (within) columns, row-by-row (direction 2).
% In other words, get a mean for each row.
rowMeansSI1 = mean(RSI1, 2, 'omitnan');

댓글을 달려면 로그인하십시오.


Muttana
Muttana 2023년 5월 6일
implement wavelet sub band coding in images using MATLAB software.
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 5월 6일
Sorry, I am perhaps a bit distracted today. Could you explain more clearly how this will help someone avoid the problem with indexing out of range?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by