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

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 日

10 投票

your_result = [];
for ii = whatever
some_vector = some_function(of_something);
your_result = [your_result; {some_vector}];
end

8 件のコメント

chels19
chels19 2016 年 7 月 8 日
Thanks, this has worked now.
José-Luis
José-Luis 2016 年 7 月 8 日
My pleasure.
Pradnya Raut
Pradnya Raut 2019 年 11 月 6 日
Is there any faster way to do this, other than above method?
ardeshir moeinian
ardeshir moeinian 2020 年 3 月 14 日
I find this a very easy and nice method... how can one do this for string arrays though?
deejt
deejt 2021 年 1 月 29 日
Thank you very much @José-Luis!
I was wondering, is there also a possibility to define within the for loop at this section (marked with %here!) that I would like to insert the results in column number xy of my result matrix (here called "your_result")?
for ii = whatever
some_vector = some_function(of_something);
your_result = [your_result; {some_vector}]; % here!
end
Muhammad Ridwanur Rahim
Muhammad Ridwanur Rahim 2021 年 7 月 18 日
this is a beautiful solution. thank you @José-Luis
Ruijie Zhu
Ruijie Zhu 2021 年 10 月 4 日
cheers mate
Mani
Mani 2024 年 5 月 8 日
Nice one.

サインインしてコメントする。

その他の回答 (3 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 7 月 8 日

0 投票

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 日

0 投票

% 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
Manuela
Manuela 2025 年 1 月 13 日

0 投票

I modified the code of the first answer in order to obtain a matrix and not a list of cells:
your_result = [];
for ii = whatever
some_vector = some_function(of_something);
your_result = cat(1,your_result,some_vector);
end

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2016 年 7 月 8 日

回答済み:

2025 年 1 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by