How to keep the previous iteration in a for loop?

7 ビュー (過去 30 日間)
Michiel Smit
Michiel Smit 2020 年 6 月 6 日
コメント済み: Michiel Smit 2020 年 6 月 6 日
Dear Reader,
I am trying to use a for loop to find the maximum daily value for water level (WL) for a hourly data set between the years 2000 and 2020. The code has been specifically written to also take shorter months (february) in to account. See below.
I want the line WL_HVH_dailymax_total a matrix keeping the [year month day WL], for example
[2000 01 01 WL; 2000 01 02 120 WL; 2000 01 03 WL ]
However, the way I have written this, it will overwrite the previous line so that the only result I obtain is:
[2020 06 05 125]
DO I need an extra line of code, or what other way can I fix this?
for i = 2000:max(WL_HVH(:,1));
WL_HVH_year = WL_HVH(WL_HVH(:,1)==i, :);
for j = 1:max(WL_HVH_year(:,2));
WL_HVH_month = WL_HVH_year(WL_HVH_year(:,2)==j, :);
for k = 1:max(WL_HVH_month(:,3));
WL_HVH_day = WL_HVH_month(WL_HVH_month(:,3)==k, :);
WL_HVH_dailymax = max(WL_HVH_day(:,6));
WL_HVH_dailymax_total = [i j k WL_HVH_dailymax];
end
end
end

採用された回答

David Hill
David Hill 2020 年 6 月 6 日
count=1;
for i = 2000:max(WL_HVH(:,1));
WL_HVH_year = WL_HVH(WL_HVH(:,1)==i, :);
for j = 1:max(WL_HVH_year(:,2));
WL_HVH_month = WL_HVH_year(WL_HVH_year(:,2)==j, :);
for k = 1:max(WL_HVH_month(:,3));
WL_HVH_day = WL_HVH_month(WL_HVH_month(:,3)==k, :);
WL_HVH_dailymax = max(WL_HVH_day(:,6));
WL_HVH_dailymax_total{count} = [i j k WL_HVH_dailymax];%make cell array
count=count+1;
end
end
end
  3 件のコメント
David Hill
David Hill 2020 年 6 月 6 日
count=1;
for i = 2000:max(WL_HVH(:,1));
WL_HVH_year = WL_HVH(WL_HVH(:,1)==i, :);
for j = 1:max(WL_HVH_year(:,2));
WL_HVH_month = WL_HVH_year(WL_HVH_year(:,2)==j, :);
for k = 1:max(WL_HVH_month(:,3));
WL_HVH_day = WL_HVH_month(WL_HVH_month(:,3)==k, :);
WL_HVH_dailymax = max(WL_HVH_day(:,6));
WL_HVH_dailymax_total(count,:) = [i j k WL_HVH_dailymax];%change to matrix
count=count+1;
end
end
end
Michiel Smit
Michiel Smit 2020 年 6 月 6 日
Thanks so much!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by