How do you save values from a for-loop in a vector?

조회 수: 2 (최근 30일)
Buttercup12
Buttercup12 2021년 3월 5일
댓글: Buttercup12 2021년 3월 5일
I have the following:
for ml=100:50:1000
z=fzero(@(x) y(x,ml), [1 10]);
disp(num2str(z))
end
But I need each value of z saved as a vector to use it in a plot later. How do you do that?

채택된 답변

Stephen23
Stephen23 2021년 3월 5일
편집: Stephen23 2021년 3월 5일
With MATLAB it is almost always better to loop over an index than to loop over data:
ml = 100:50:1000;
n = numel(ml);
z = nan(1,n); % preallocate
for k = 1:n % loop over indices
z(k) = fzero(@(x) y(x,ml(k)), [1,10]);
end %^^^ indexing ^^^

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by