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!