Reputation: 5
I am trying to add a row to a matrix every time an iterative loop in MATLAB produces an answer from fsolve function.
Say, fsolve produces an answer 3 and 2 (2 elements), and then I want to add these into a 1x2 matrix.
After a second loop fsolve produces an answer 5 and 3 (2 new elements) and I want to add these to the old solution matrix but as a new row so that a new matrix is a 2x2 matrix.
and on and on.
any ideas?
Upvotes: 0
Views: 134
Reputation: 99
You can also use end
to add an extra column to your matrix, so A(:,end+1) = [x1; x2];
adds an extra column to the matrix A. This also works for rows of course.
Upvotes: 1
Reputation: 29064
To augment:
Before the loop:
A = [];
In the loop, for example:
A = [A; 3 2];
A better way is to pre-allocate the array given you know how many times you gonna run the loop.
For example,
A = zeros(n,2);
A(i,:) = [3 2];
Upvotes: 1