Is vectorizing this even possible?
Afficher commentaires plus anciens
vec3(1) = 1;
i = 1;
while i<5
i = i+1;
vec3(i) = (vec3(i-1)+2)^2;
end
vec3
Réponse acceptée
Plus de réponses (1)
madhan ravi
le 17 Sep 2020
Modifié(e) : madhan ravi
le 17 Sep 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 commentaires
Stephen23
le 17 Sep 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
le 17 Sep 2020
Ah thanks Stephen!
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!