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

155 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

回答(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 个评论
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