Effacer les filtres
Effacer les filtres

Trouble with saving outer loop values

1 vue (au cours des 30 derniers jours)
klb
klb le 3 Mar 2021
Commenté : klb le 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!

Réponse acceptée

David Hill
David Hill le 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
  1 commentaire
klb
klb le 4 Mar 2021
Works. Thank David!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by