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

1 visualizzazione (ultimi 30 giorni)
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!!

Risposta accettata

Mathieu NOE
Mathieu NOE il 30 Nov 2022
Modificato: Mathieu NOE il 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 Commenti
Ashfaq Ahmed
Ashfaq Ahmed il 30 Nov 2022
Modificato: Ashfaq Ahmed il 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 il 1 Dic 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by