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

1 view (last 30 days)
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!!

Accepted Answer

Mathieu NOE
Mathieu NOE on 30 Nov 2022
Edited: Mathieu NOE on 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 Comments
Ashfaq Ahmed
Ashfaq Ahmed on 30 Nov 2022
Edited: Ashfaq Ahmed on 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 on 1 Dec 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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!