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

조회 수: 1(최근 30일)
Ashfaq Ahmed 2022년 11월 30일
댓글: Mathieu NOE 2022년 12월 1일
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!!

댓글을 달려면 로그인하십시오.

### 채택된 답변

Mathieu NOE 2022년 11월 30일
편집: Mathieu NOE 2022년 11월 30일
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표시숨기기 이전 댓글 수: 1
Mathieu NOE 2022년 12월 1일
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

댓글을 달려면 로그인하십시오.

### 범주

Find more on Matrix Indexing 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!

Translated by