Matlab - insert/append rows into matrix iteratively

Andrius picture Andrius · Dec 15, 2013 · Viewed 59.5k times · Source

How in matlab I can interactively append matrix with rows?

For example lets say I have empty matrix:

m = [];

and when I run the for loop, I get rows that I need to insert into matrix.

For example:

for i=1:5
  row = v - x; % for example getting 1 2 3
  % m.append(row)?
end

so after inserting it should look something like:

m = [
     1 2 3
     3 2 1
     1 2 3
     4 3 2
     1 1 1
]

In most programming languages you can simply append rows into array/matrix. But I find it hard to do it in matlab.

Answer

lennon310 picture lennon310 · Dec 15, 2013

m = [m ; new_row]; in your loop. If you know the total row number already, define m=zeros(row_num,column_num);, then in your loop m(i,:) = new_row;