필터 지우기
필터 지우기

How do you append to a matrix within a for loop?

조회 수: 275 (최근 30일)
chels19
chels19 2016년 7월 8일
댓글: Mani 2024년 5월 8일
Hi, I am using a for loop to process data (this part works fine). What I need to do is take the matrix (A) and after each loop update A to create one matrix. For instance, in the image below A is produced on the first loop, during loop 2 A "grows" to include the data from loop 1 and the data from loop 2, and so on.
Loop 1 produces a matrix, on the next iteration I need to append to this matrix the results of that loop, and so on until all of the data is processed.
It should be noted that the number of rows on each loop is unknown. However, the number of columns is fixed to 7.
Any ideas would be greatly appreciated please.

채택된 답변

José-Luis
José-Luis 2016년 7월 8일
편집: José-Luis 2016년 7월 8일
your_result = [];
for ii = whatever
some_vector = some_function(of_something);
your_result = [your_result; {some_vector}];
end
  댓글 수: 8
Ruijie Zhu
Ruijie Zhu 2021년 10월 4일
cheers mate
Mani
Mani 2024년 5월 8일
Nice one.

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

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 8일
A=[1 2;3 4]
B=[4 5;6 7]
A=[A;B]
  댓글 수: 1
chels19
chels19 2016년 7월 8일
Thank you but this just replaces the data within the loop. I need to update A on every iteration so that it grows.

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


SRIAKILAN S A
SRIAKILAN S A 2023년 2월 21일
% make an empty array
% the main command is end+1
N=[];
for i=0:10
x=i*i;
N(end+1)=[x]
end
N = 0
N = 1×2
0 1
N = 1×3
0 1 4
N = 1×4
0 1 4 9
N = 1×5
0 1 4 9 16
N = 1×6
0 1 4 9 16 25
N = 1×7
0 1 4 9 16 25 36
N = 1×8
0 1 4 9 16 25 36 49
N = 1×9
0 1 4 9 16 25 36 49 64
N = 1×10
0 1 4 9 16 25 36 49 64 81
N = 1×11
0 1 4 9 16 25 36 49 64 81 100

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by