MATLAB Answers

Finding minima using for loop and if construct

1 view (last 30 days)
Elijah L
Elijah L on 16 Sep 2020
Commented: Star Strider on 16 Sep 2020
I have a 205x1 matrix of data points called zpoint. I need to write a script using a 'for' loop and an 'if' construct to identify all minima in zpoint.
I want the script to utilize the fact that the previous and next points around the minima points will be greater than it. (possibly using localmin)
This is the start of my script:
n = length(zpoint)
for i = 1:n
if zpoint(i) ....
end
  4 Comments
Star Strider
Star Strider on 16 Sep 2020
Note that one of the two duplicate postings (three total) of this has an Accepted Answer: Finding minima using if and for loops

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 16 Sep 2020
You also need to start at 2 and end at the end-1
for i = 2 : length(zpoint)-1
if zpoint(i) < zpoint(i+1) && zpoint(i) < zpoint(i-1)
end
end
And your zmn is not really used for anything so you can get rid of it.
Also, you might want to take a look at movmin(), and imregionalmin() which do the same thing.

Community Treasure Hunt

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

Start Hunting!