Saving only the last loop

Hi, I'm very new to MATLAB (introduced to me this first term at uni), and I have been struggling with trying to save each loop in a column that is preallocated as 135x1 using zeros()
This is my code for the section I am stuck on:
%% Preallocating memory
dischargemax=zeros((winterdischarge(end,1)-winterdischarge(1,1)),1);
yearcolumn=winterdischarge(:,1);
%% Looping to find max of each year (Full year of 1883 not given, only months Nov and Dec.)
%% 1883
dischargemax(1,1)=max(winterdischarge(yearcolumn==1883,4));
%% other years
for i=2:length(leapyear)
for ii = 1884:2018
extract=winterdischarge(yearcolumn == ii,4);
end
dischargemax(i,1)=max(extract);
end
In ans I have the first max of the first year in (1,1) and then every other row is the max extract of the last year. How would I fix this? Also how would I set the output to dischargemax instead of ans?
Thank you.

回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 12 月 18 日
編集済み: KALYAN ACHARJYA 2019 年 12 月 18 日

0 投票

Do the proper indexing
May be
for i=2:length(leapyear)
for ii = 1884:2018
extract(ii)=winterdischarge(yearcolumn==ii,4);
end
dischargemax(i)=max(extract);
end
See the following example-
for k=1:n
result=...??
end
This code reflects the the last iteration results only (Result having last data), otherwise
for k=1:n
result(k)=...??
end
It holds the all output in result 1 D array, do as per your requiremnets.
Any issue let me know.

1 件のコメント

Neill Harris
Neill Harris 2019 年 12 月 18 日
Hi,
Thank you for the speedy reply.
After implementing your code I get the following error.
Subscripted assignment dimension mismatch.
Error in MaxDischargeWinter (line 22)
extract(ii)=winterdischarge(yearcolumn==ii,4);

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2019 年 12 月 18 日

コメント済み:

2019 年 12 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by