Trouble with saving outer loop values

2 views (last 30 days)
klb
klb on 3 Mar 2021
Commented: klb on 4 Mar 2021
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!

Accepted Answer

David Hill
David Hill on 3 Mar 2021
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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!