How do I fix error: Index exceeds matrix dimensions?

I want to make a loop where I calculate the mean of my 35040x12datamatrix for every 24 datapoint, but I get an error: Error using dataset/subsref (line 616) Index exceeds matrix dimensions.
for i=1:24:35040
Xday(icount,:)=mean(A.data(i:24*i,:));
icount=icount+1;
end
What am I doing wrong? Thanks

回答 (2 件)

James Tursa
James Tursa 2016 年 2 月 3 日
編集済み: James Tursa 2016 年 2 月 3 日

0 投票

Try this:
for i=1:24:35040
Xday(icount,:)=mean(A.data(i:(i+23),:)); % <-- Changed 24*i to i+23
icount=icount+1;
end
Or a vectorized way:
Xday = reshape(mean(reshape(A.data,24,[])),1460,[]);
Lilja Dahl
Lilja Dahl 2016 年 2 月 3 日

0 投票

Fantastic! The loop didn't work but vector did! thank you so much

2 件のコメント

James Tursa
James Tursa 2016 年 2 月 3 日
編集済み: James Tursa 2016 年 2 月 3 日
For the loop, did you preallocate Xday? E.g., Xday = zeros(1460,12). And did you initialize icount = 1?
Lilja Dahl
Lilja Dahl 2016 年 2 月 12 日
Thank you so much for your help. My loop ended up looking like this:
icount=1;
for i=1:24:35040
Xday(icount,:)=missmean(Anew0.data(i:icount*24,:));
icount=icount+1;
end

この質問は閉じられています。

質問済み:

2016 年 2 月 3 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by