フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

1 回表示 (過去 30 日間)
Camilla Lincetto
Camilla Lincetto 2015 年 2 月 6 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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 件のコメント
Camilla Lincetto
Camilla Lincetto 2015 年 2 月 7 日
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.

回答 (2 件)

James Tursa
James Tursa 2015 年 2 月 6 日
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 件のコメント
Stephen23
Stephen23 2015 年 2 月 9 日
編集済み: Stephen23 2015 年 2 月 9 日
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 2015 年 2 月 9 日
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 2015 年 2 月 7 日
編集済み: Stephen23 2015 年 2 月 8 日
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 件のコメント
Camilla Lincetto
Camilla Lincetto 2015 年 2 月 7 日
Matlab shows this error message: Subscripted assignment dimension mismatch. what does it mean?
Stephen23
Stephen23 2015 年 2 月 8 日
編集済み: Stephen23 2015 年 2 月 8 日
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.

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by