Is vectorizing this even possible?
조회 수: 1 (최근 30일)
이전 댓글 표시
vec3(1) = 1;
i = 1;
while i<5
i = i+1;
vec3(i) = (vec3(i-1)+2)^2;
end
vec3
댓글 수: 0
채택된 답변
Walter Roberson
2020년 9월 17일
I posted the complete vectorization several days ago; . Unfortunately the person deleted the question.
syms v v0 c
f(v) = (v+c)^2;
f5 = f(f(f(f(f(v0)))));
vec3_5 = expand(subs(f5, c, 2));
v0^32 + 64*v0^31 + 2016*v0^30 + 41600*v0^29 + 631536*v0^28 + 7511168*v0^27 + 72782528*v0^26 + 590011136*v0^25 + 4077667064*v0^24 + 24363708032*v0^23 + 127184607424*v0^22 + 584772138240*v0^21 + 2382767071968*v0^20 + 8644745151232*v0^19 + 28021844462720*v0^18 + 81349497514496*v0^17 + 211814884610908*v0^16 + 494935571753856*v0^15 + 1037540400943680*v0^14 + 1949025086827264*v0^13 + 3273934344609568*v0^12 + 4902203714779904*v0^11 + 6514485357242496*v0^10 + 7638211784159744*v0^9 + 7840967227104336*v0^8 + 6975721989473536*v0^7 + 5305860461727104*v0^6 + 3387252771621376*v0^5 + 1768336935606208*v0^4 + 726328276999680*v0^3 + 220554340195584*v0^2 + 44118436709376*v0 + 4371938082724
Where v0 = 1
댓글 수: 3
Walter Roberson
2020년 9월 18일
Yup ;-)
Using a for loop is not vectorizing . This solution is not vectorized in terms of the number of iterations, but it is vectorized in terms of different initial conditions.
I think it should be possible to calculate what all the terms should be, in terms of binomial coefficients and number of iterations, but the form is not coming to mind immediately.
추가 답변 (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
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!