How to store output of a for loop in a matrix?

조회 수: 1 (최근 30일)
Jamil Dudhwala
Jamil Dudhwala 2019년 2월 18일
댓글: madhan ravi 2019년 2월 18일
Hi guys,
I'm having difficulty storing my output values from a loop into a matrix. And whenever I try the solution mentioned in other examples the error preallocate comes up.
My code is below:
P= [10 20 30]
A=1230
B=0.14
Idir=0
for index = 1:3
Idir=(A/(exp(B/sin(P(index)))))
end

채택된 답변

madhan ravi
madhan ravi 2019년 2월 18일
Idir=zeros(size(P)); % this is how you preallocate before loop
Note: In this case you don’t need a loop.
  댓글 수: 5
Jamil Dudhwala
Jamil Dudhwala 2019년 2월 18일
Thank you, that worked.
But in that case, in MATLAB, when should for loops be used?
madhan ravi
madhan ravi 2019년 2월 18일
Definitely I would suggest you to read the link suggested by stephen , once if you get familiarized with all the functions then you will know
When to use it ?
How to use it ?
Where to use it ?

댓글을 달려면 로그인하십시오.

추가 답변 (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