MATLAB: How to store output of a for loop in a matrix

for loopoutput

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

Best Answer

Idir=zeros(size(P)); % this is how you preallocate before loop
Note: In this case you don’t need a loop.
Related Question