Calculating N-th Element Of A Sequence

2 ビュー (過去 30 日間)
Harel Harel Shattenstein
Harel Harel Shattenstein 2015 年 4 月 12 日
回答済み: Image Analyst 2015 年 4 月 12 日
function m = seq( x )
n=[(1:x);(1:x)];
if x==1
n(1,1)=2;
end
if x==2
n(1,2)=3;
end
n(1,x)=(1-seq(x-1))./seq(x-1)+seq(x-2);
m=n(1,x);
end
I am get a stack overflow error, what am I doing wrong?

回答 (1 件)

Image Analyst
Image Analyst 2015 年 4 月 12 日
You're recursing in too deeply - calling it too many times. Your function does not appear to have any "end" condition where it will "back out" of this really deep recursion you're doing, like a line at the start that says something like if x <= 0 return or something like that. Think about it a little, and you'll figure it out.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by