I am getting an Index in position 1 exceeds array bounds error
198 ビュー (過去 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 件のコメント
Image Analyst
2022 年 2 月 26 日
for i = 1 : height(app.UITable.Data)
E(i) = app.UITable.Data{i, 1}
回答 (3 件)
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.
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.
0 件のコメント
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
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
2023 年 5 月 6 日
implement wavelet sub band coding in images using MATLAB software.
1 件のコメント
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?
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!