Easy question - How can I save this variable within the for loop?

1 vue (au cours des 30 derniers jours)
Ashfaq Ahmed
Ashfaq Ahmed le 30 Nov 2022
Commenté : Mathieu NOE le 1 Déc 2022
Hi!
I have one easy question, but weridly I am not being to solve the problem. Say I have this very simple code -
for n = 1:10
A = [1 n 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
B{n} = A(1,2)*10;
end
How can I save all ten As in a cell/structure? (Just like the code save every updated B)
Thank you!!

Réponse acceptée

Mathieu NOE
Mathieu NOE le 30 Nov 2022
Modifié(e) : Mathieu NOE le 30 Nov 2022
hello
If I understand correctly , n must be multiple of 10 and that's the only values of interest for A and B, no need to perform other n values computations that are not multiple of 10
you can simply do that
for k = 1:10
n = k*10;
A = [1 n 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
B{k} = A(1,2)*10;
end
B
B = 1×10 cell array
{[100]} {[200]} {[300]} {[400]} {[500]} {[600]} {[700]} {[800]} {[900]} {[1000]}
  2 commentaires
Ashfaq Ahmed
Ashfaq Ahmed le 30 Nov 2022
Modifié(e) : Ashfaq Ahmed le 30 Nov 2022
Hi! Yes, you got the point right. But the code is not saving the updated matrix A everytime. I want all 10 A's to be saved!
Just like all the B's are saved in a cell array.
Mathieu NOE
Mathieu NOE le 1 Déc 2022
hello again
so you want this ?
for k = 1:10
n = k*10;
A = [1 n 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
B{k} = A;
end
B{1}
ans = 4×4
1 10 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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