Is vectorizing this even possible?
Mostrar comentarios más antiguos
vec3(1) = 1;
i = 1;
while i<5
i = i+1;
vec3(i) = (vec3(i-1)+2)^2;
end
vec3
Respuesta aceptada
Más respuestas (1)
madhan ravi
el 17 de Sept. de 2020
Editada: madhan ravi
el 17 de Sept. de 2020
A simple for loop is the best and easier to understand:
vec3 = zeros(5,1);
vec3(1) = 1;
for k = 2:5 % edited after Stephen’s comment
vec3(k) = (vec3(k-1)+2)^2;
end
vec3
2 comentarios
Stephen23
el 17 de Sept. de 2020
Starting the for loop from one will throw an error. Better to start from two:
vec3 = ones(5,1);
for k = 2:5
vec3(k) = (vec3(k-1)+2)^2;
end
madhan ravi
el 17 de Sept. de 2020
Ah thanks Stephen!
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!