How to successively fill a vector with for loop?

72 visualizaciones (últimos 30 días)
Daniel Gray
Daniel Gray el 4 de Feb. de 2018
Editada: Stephen23 el 4 de Feb. de 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

Respuesta aceptada

Stephen23
Stephen23 el 4 de Feb. de 2018
Editada: Stephen23 el 4 de Feb. de 2018
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

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by