How to save maximum value in each iteration

6 ビュー (過去 30 日間)
Moe
Moe 2014 年 4 月 30 日
編集済み: per isakson 2014 年 4 月 30 日
Hi everyone,
Suppose I have a for-loop:
a=rand(1);
n(:,:,1) = zeros(1,1);
for i = 2:4
n(:,:,i) = n(:,:,i-1) + a
[val,idx] = max(n);
end
How can I have a list of maximum values in each iteration, such as:
s = [2 3;3 5;4 7] % iteration "2" maximum value is "3"; iteration "3" maximum value is "5"; iteration "4" maximum value is "7"
Can anyone please help me?

採用された回答

per isakson
per isakson 2014 年 4 月 30 日
編集済み: per isakson 2014 年 4 月 30 日
You overwrite val and idx. To avoid that, replace
[val,idx] = max(n);
by
[val(i),idx(i)] = max(n);
  1 件のコメント
Moe
Moe 2014 年 4 月 30 日
Perfect. Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by