Changing a for loop to recursion
显示 更早的评论
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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!