フィルターのクリア

My function doesn't append to it's list

2 ビュー (過去 30 日間)
Kacper
Kacper 2024 年 5 月 10 日
コメント済み: Kacper 2024 年 5 月 12 日
%calculates polynomials of form Pk = ((2k-1)t/k)*Pk-1 - ((k-1)/k)*Pk-2
function [poly, filllist] = calc_poly (n, t, filllist)
if n == 0
filllist = [filllist; ones(size(t))];
poly = ones(size(t));
return
end
if n == 1
filllist = [filllist; t];
poly = t;
return
end
poly = ((2*n-1)/n)*t.*calc_poly(n-1, t, filllist) - ((n-1)/n)*calc_poly(n-2, t, filllist);
filllist = [filllist; poly];
end
This function is supposed to recursively calculate the polys and append them into filllist, but the array remians unalatered after running it. I know that as it stands there would be duplicates in the list, but I first want to get the function to actually do anything.

回答 (1 件)

Steven Lord
Steven Lord 2024 年 5 月 10 日
Your recursive calls only call your function with one output argument, so the modified second output argument is not returned from that function. Therefore any changes you make to your third input argument are discarded when each recursive call returns.
  6 件のコメント
Walter Roberson
Walter Roberson 2024 年 5 月 12 日
Experiment with creating a script that has both parts together, and running the script. You can use the "Copy" box on the code I posted to get the code to be pasted into the script.
Kacper
Kacper 2024 年 5 月 12 日
I pasted your code to a new script and it worked, despite the previous script being the same. huh. Anyway, thx for your answers!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by