Info

This question is closed. Reopen it to edit or answer.

Index exceeds Matrix Dimensions

1 view (last 30 days)
Alexander Sun
Alexander Sun on 18 Mar 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
I am trying to calculate the mean value (avoiding NaN) for every 12 pages of elements out of a total 180*90*1657 three dimensional array. My method is to use loop to calculate the mean whenever it counts to 194400 elements (180*90*12). However, the command window displays index exceeds Matrix Dimensions for me and indicate the problematic code is
tsamean(i)=nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(tsurfano(index(i):index(i+1))))))))))))));
The variable "tsamean" is still created with expected value yet How do I clear that error? Here is the code, tsurfano is the three dimensional array with all those elements and the 26843400 is 180*90*1657 indicating the total amount of elements in the array.
index=1:194400:26843400;
for i=1:length(index);
tsamean(i)=nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(nanmean(tsurfano(index(i):index(i+1))))))))))))));
end
tsamean

Answers (1)

Rik
Rik on 18 Mar 2018
Your code fails on the last loop: it tries to reach index(i+1), but i is equal to the length of index on the last loop.
You might be better off using a more direct measure with sum, especially since the mean of means is not necessarily equal to the mean of the total (if the subgroups are not of equal length).
Also, 1657 is not a multiple of 12, so I changed it to 1656.
tsurfano=randi([1 10],180,90,1656);
pagesums=squeeze(sum(sum(tsurfano,1,'omitnan'),2,'omitnan'));
pageelem=squeeze(sum(sum(~isnan(tsurfano),1),2));
pagesums=reshape(pagesums,12,(numel(pagesums))/12);
pageelem=reshape(pageelem,12,(numel(pageelem))/12);
pagesums=sum(pagesums,1);
pageelem=sum(pageelem,1);
pagemean=pagesums./pageelem;
  1 Comment
Rik
Rik on 26 Mar 2018
If you found this answer useful, please mark it as accepted answer. It will make it easier for other people with the same question to find an answer, as well as give me reputation points. If this didn't solve your question, please comment with what problems you are still having.

Community Treasure Hunt

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

Start Hunting!