Stop for-loop at the second last column
2 次查看(过去 30 天)
显示 更早的评论
E.g. I have a vector a = [1 1 5 1 1 1 10 2]; Error appeared when the loop reached the last column. I am writing to stop at end-1, but kinda lost. Please help solve this. thanks in advance.
function z = findpeaks(a)
for i = 2:[a(:,end-1)]
z = a(a(i)>a(i-1) & a(i)>a(i+1))
end
2 个评论
Dennis
2019-4-17
I think your loop has a creative synthax (not wrong though).
What bothers me is that your loop runs from 2 to the 2nd last value in a, so the amount of iterations depend on your vector entry:
a = [1 1 5 1 1 1 10 2]; %10 iterations
a = [1 1 5 1 1 1 1 2]; %1 iteration
a = [1 1 5 1 1 1 -10 2]; %does nothing
a = [1 1 5 1 1 1 0.5 2]; %does nothing
I am not sure if this is intentional.
Also i recommened to rename your function since findpeaks() is already implemented in matlab.
Maybe you should check the Matlab version out, i guess you want to try something similar:
[peakval,peakloc]=findpeaks(a)
peakval =
5 10
peakloc =
3 7
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!