Save max of each iteration

2 ビュー (過去 30 日間)
Moe
Moe 2014 年 5 月 5 日
コメント済み: Moe 2014 年 5 月 5 日
Suppose that matrix A have new value in each iteration (updatable), Is there anyway to save the rowsOfMaxes matrix A in each iteration? I tried to use [rowsOfMaxes(i) colsOfMaxes(i)], but it doesn't work.
A = [6;7;21;4;9;21;5;1];
max(A(:))
[maxValue, linearIndexesOfMaxes] = max(A(:));
[rowsOfMaxes colsOfMaxes] = find(A == maxValue,1,'first')
Can anyone please help me?

採用された回答

Image Analyst
Image Analyst 2014 年 5 月 5 日
It works for me:
for k = 1 : 10
A = randi(9, 8, 1) % Different 8x1 array each iteration.
max(A(:))
[maxValue, linearIndexesOfMaxes] = max(A(:));
% [rowsOfMaxes(k) colsOfMaxes] = find(A == maxValue) % Won't work
[rowsOfMaxes(k), colsOfMaxes] = find(A == maxValue, 1, 'first')
end
What I suspect is that you're not using 'first', and that may not work. If the max happens at two locations, for example you have 21 twice, then rowsOfMaxes will be two numbers (3 and 6). So you can't stuff two numbers into a single element like rowsOfMaxes(i). Since rowsOfMaxes could have different lengths depending on which iteration you're on and what the values of A happen to be during that iteration, you'll have to have rowsOfMaxes be a cell array, which can handle different sized arrays, unlike a regular numerical array which must be rectanglar.
  1 件のコメント
Moe
Moe 2014 年 5 月 5 日
Thanks a lot!

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

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by