How to eliminate the for loop but end with the same result
    2 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Bryce McCord
 el 2 de Sept. de 2022
  
    
    
    
    
    Respondida: Star Strider
      
      
 el 2 de Sept. de 2022
            A = input('Using brackets, enter a vector: ')
for i=1:length(A)
    if(A(i) > 0)
        B(i) = A(i).^3;
    else 
        B(i) = 0;
    end
end
B
How can I convert this code so that when the for loop and if statement are deleted the code still has the same output?
0 comentarios
Respuesta aceptada
  Star Strider
      
      
 el 2 de Sept. de 2022
        Try something like this — 
A = -3:3;
B = A.^3.*(A>0)
It uses a version of ‘logical indexing’.  
.
0 comentarios
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!

