How do you save values from a for-loop in a vector?
    3 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Buttercup12
 el 5 de Mzo. de 2021
  
    
    
    
    
    Comentada: Buttercup12
 el 5 de Mzo. de 2021
            I have the following: 
for ml=100:50:1000 
    z=fzero(@(x) y(x,ml), [1 10]);
    disp(num2str(z))
end
But I need each value of z saved as a vector to use it in a plot later.  How do you do that?
0 comentarios
Respuesta aceptada
  Stephen23
      
      
 el 5 de Mzo. de 2021
        
      Editada: Stephen23
      
      
 el 5 de Mzo. de 2021
  
      With MATLAB it is almost always better to loop over an index than to loop over data:
ml = 100:50:1000;
n = numel(ml);
z = nan(1,n); % preallocate
for k = 1:n % loop over indices
    z(k) = fzero(@(x) y(x,ml(k)), [1,10]);
end %^^^     indexing       ^^^
Más respuestas (0)
Ver también
Categorías
				Más información sobre Loops and Conditional Statements 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!