assigning output of a for loop to an array

i want to put the 6 values of W into an array but i cant sry fr the silly question
E = 10;
Rt = 1000;
Rm = 0.01;
for Ri = (0:200:100)
W = error_calculation (Ri,Rt,Rm,E);
end

 Accepted Answer

Adam
Adam on 7 Dec 2016
Edited: Adam on 7 Dec 2016
W(Ri) = error_calculation (Ri,Rt,Rm,E);
will put the values into an array, assuming the output of error_calculation is a scalar. If not then you need to use W{Ri} for a cell array or you can assign to a multi-dimensional numerical array if the size of the result is always the same.
Though you should presize this before the loop.
I assume this was a typo:
for Ri = (0:200:100)
and you meant to loop up to 1000 to get your 6 values.
W = zeros( 1, 6 )
works for presizing, although I don't like hard-coding an assumption in two independent places so I would maybe create the 0:200:1000 vector first and use numel to get its size to presize the result.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!