フィルターのクリア

Trouble with saving outer loop values

1 回表示 (過去 30 日間)
klb
klb 2021 年 3 月 3 日
コメント済み: klb 2021 年 3 月 4 日
Hi eveyone,
Tying to save outer loop iteration. Code works as wanted till first comment
clear all
A = [12 4;
1, 25;
4,19]
B = [90, 85;
60, 50;
90,40]
maxval=[];
for w = 1:5
for r = 1: size(A,1)
run1 = 2*A(r,1)+ r*B(r,1);
run2 = 3*A(r,2)+ B(r,2);
mat(r,:) = [run1, run2 ];
end
mat
[what , where] = max(sum((mat),2))
maxval = [w,mat(where,:)]
end
% here on is the bad code bit. tying to save/pass on the 'maxval' for each outer loop iteration.
% tried this:
maxval= [maxval;maxval]
And got:
5 278 97
5 278 97
% also tried
maxval (w,:) = maxval
and got :
[ 5 278 97
0 0 0
0 0 0
0 0 0
5 278 97]
Niether are correct.
Expected output is not those but this:
[ 1 278 97
2 278 97
3 278 97
4 278 97]
5 278 97]
What am I not doing right ?
Thank for your time!

採用された回答

David Hill
David Hill 2021 年 3 月 3 日
A = [12 4;
1, 25;
4,19];
B = [90, 85;
60, 50;
90,40];
maxval=[];
for w = 1:5
for r = 1: size(A,1)
run1 = 2*A(r,1)+ r*B(r,1);
run2 = 3*A(r,2)+ B(r,2);
mat(r,:) = [run1, run2 ];
end
[what , where] = max(sum((mat),2));
maxval(w,:) = [w,mat(where,:)];
end
  1 件のコメント
klb
klb 2021 年 3 月 4 日
Works. Thank David!

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

その他の回答 (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