필터 지우기
필터 지우기

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

추가 답변 (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