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

1 visualización (últimos 30 días)
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!!

Respuesta aceptada

Mathieu NOE
Mathieu NOE el 30 de Nov. de 2022
Editada: Mathieu NOE el 30 de Nov. de 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 comentarios
Ashfaq Ahmed
Ashfaq Ahmed el 30 de Nov. de 2022
Editada: Ashfaq Ahmed el 30 de Nov. de 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 el 1 de Dic. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by