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.
Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
How can I insert vectors created in a for loop in a matrix?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (2개)
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
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
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
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
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!