Clear Filters
Clear Filters

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

155 views (last 30 days)
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 Comments

Sign in to comment.

Answers (3)

Cris LaPierre
Cris LaPierre on 19 Dec 2018
Edited: Cris LaPierre on 19 Dec 2018
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 on 19 Dec 2018
When you do this:
MeanSI1 = nanmean(RSI1(1:22,:),1);
It's saying that RSI1 has only 1 row, not 22.
  2 Comments
Image Analyst
Image Analyst on 14 Nov 2021
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');

Sign in to comment.


Muttana
Muttana on 6 May 2023
implement wavelet sub band coding in images using MATLAB software.
  1 Comment
Walter Roberson
Walter Roberson on 6 May 2023
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?

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!