Is vectorizing this even possible?
이전 댓글 표시
vec3(1) = 1;
i = 1;
while i<5
i = i+1;
vec3(i) = (vec3(i-1)+2)^2;
end
vec3
채택된 답변
추가 답변 (1개)
madhan ravi
2020년 9월 17일
편집: madhan ravi
2020년 9월 17일
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
Stephen23
2020년 9월 17일
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
2020년 9월 17일
Ah thanks Stephen!
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!