Changing a for loop to recursion
Ältere Kommentare anzeigen
Just wondering if I could get a bit of help on this. How would I change this code to remove the for loop so it operates recursively.
function [x, y] = test(data)
c=1;
input=data;
x=length(input);
for z=2:x
if input(z-1)>input(z) && input(z)<input(z+1)
x(c,1)=z;
y(c,1)=input(z);
c=c+1
end
end
end
So far I have tried to change it and gotten
function [x, y] = test(data)
c=1;
input=data;
x=length(input);
if length(input(z-1))+2>length(input)
return
elseif input(z-1)>input(z) && input(z)<input(z+1)
x(c,1)=z;
y(c,1)=input(z);
c=c+1
end
end
end
But this just runs the if statement once and does not operate recursively.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Performance and Memory finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!