Calling a function f inside f,how can we do this
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
function H=f(m)
H1=zeros(m/2);
H=[f(m/2) H1;H1 H1];
end
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 22 Apr. 2013
Bearbeitet: Walter Roberson
am 22 Apr. 2013
What you have will call f from inside f (which is called "recursion")
However, your code does not have any protections for the case that "m" is odd, and does not have any way to stop iterating when you get down to m == 1.
Weitere Antworten (1)
Babak
am 22 Apr. 2013
Just as a side issue: You better change the name of the function from a one letter name "f" to at least a 2 letter name. I remember I was trying to compute function of a function and MATLAB has issues (bugs) in calculating the result..(not sure if it's already fixed or not)... but things like sin(f(2)) or g(f(x)) may fail for a generic f function even if f is correctly defined.
This has nothing to do with the "recursion" Walter is talking about.
2 Kommentare
Walter Roberson
am 23 Apr. 2013
I have not heard of any issue with single-letter names. Do you have the bug report number, or a link to where you have posted about this before?
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!