MATLAB Answers

0

How to successively fill a vector with for loop?

Asked by Daniel Gray on 4 Feb 2018
Latest activity Edited by Stephen Cobeldick on 4 Feb 2018
I have my code set up as follows:
thmaxn = [];
for ang=[0, pi/2, pi, 3*pi/2, 2*pi]
thmax = phasedist(ang,N,rhoss);
end
Don't worry what the function does, it just gives a value for each angle. I just wondered how I could put each value into the thmaxn vector successively?
Thanks

  0 Comments

Sign in to comment.

1 Answer

Answer by Stephen Cobeldick on 4 Feb 2018
Edited by Stephen Cobeldick on 4 Feb 2018
 Accepted Answer

Preallocate the output array and then simply use indexing:
ang = 0:pi/2:2*pi;
thmax = zeros(1,numel(ang));
for k = 1:numel(ang)
thmax(k) = phasedist(ang(k),N,rhoss);
end

  0 Comments

Sign in to comment.