Trouble with saving outer loop values

2 次查看(过去 30 天)
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by