Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

How can I insert vectors created in a for loop in a matrix?

1 view (last 30 days)
I need to insert the results of 11 iterations of for-loop into a Matrix. If I run the next commands, Matlab creates a variable wp that contains only the vector of the last iteration.
for G=0:0.5:5
wp=(((c*G*i_covrend*E)-(b*G*i_covrend*uno))/((a*c)-(b^2)))-pesi_bench
end
I'm sorry but I don't know very well Matlab. Thank you!
  1 Comment
Camilla Lincetto
Camilla Lincetto on 7 Feb 2015
I need to insert the results of 11 iterations of for-loop into a Matrix or to create 11 different variables. If I run the previuos commands, I ask Matlab to do 11 iterations in which calculates each time a new vector (wp). At the end Matlab creates one variable wp corresponding only to the last vector calculated , but I lose the first 10 vectors.

Answers (2)

James Tursa
James Tursa on 6 Feb 2015
E.g.,
wpvector = 0:0.5:5; % Create a vector the same size as the loop range
k = 0; % Initialize the index into this vector
for G=0:0.5:5
wp=(((c*G*i_covrend*E)-(b*G*i_covrend*uno))/((a*c)-(b^2)))-pesi_bench
k = k + 1; % Increment the index
wpvector(k) = wp; % Save this iteration result into the vector
end
  5 Comments
Stephen23
Stephen23 on 9 Feb 2015
Edited: Stephen23 on 9 Feb 2015
Actually this proposed answer does not work.
The variable wpvector is initialized as a vector, and then during each loop iteration the assignment wpvector(k) = wp; attempts to force a vector of values into a scalar position. This will always result in an error, unless wpvector is preallocated to the correct size matrix AND the correct indexing is used (e.g. wpvector(k,:)), OR if wp is a scalar (which we are told it is not).
James Tursa
James Tursa on 9 Feb 2015
Precisely, which is why I asked for the size of the column vector so that proper pre-allocation could be done (I was waiting for an answer so that I could edit).

Stephen23
Stephen23 on 7 Feb 2015
Edited: Stephen23 on 8 Feb 2015
Try this:
vec = 0:0.5:5;
for k = numel(vec):-1:1
G = vec(k);
wp(k,:) = (((c*G*i_covrend*E)-(b*G*i_covrend*uno))/((a*c)-(b^2)))-pesi_bench
end
The reverse-order of the sequence of k is important, because it preallocates the matrix wp.
  2 Comments
Camilla Lincetto
Camilla Lincetto on 7 Feb 2015
Matlab shows this error message: Subscripted assignment dimension mismatch. what does it mean?
Stephen23
Stephen23 on 8 Feb 2015
Edited: Stephen23 on 8 Feb 2015
You wrote that (((c*G*i_covrend*E)-(b*G*i_covrend*uno))/((a*c)-(b^2)))-pesi_bench returns a column vector. Lets try my code with a simple column vector:
vec = 0:0.5:5;
for k = numel(vec):-1:1
G = vec(k);
wp(k,:) = G+[0;1;100];
end
This code works without error for me. It is likely that your variable wp is being predefined somewhere before the loop: you need to remove any preallocation of wp or assignment to wp before this loop. Only then will the code given in my answer work correctly.
Indeed, when I define wp before the loop with the wrong number of dimensions, it also results in the error ??? Subscripted assignment dimension mismatch. The solution is to ensure that your code does not define wp before the loop. You can also use clear to remove variables from the workspace.

This question is closed.

Community Treasure Hunt

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

Start Hunting!