How do you find the maximum value within segments of a vector?

16 views (last 30 days)
If I have a vector with say, 10 elements, such as vec = [11 23 34 60 58 65 55 42 35 21], is there a way to find the maximum value between specified locations within the vector? For example, I want to find the location between locations 1 and 5 that has the maximum value. So, the answer would be 4.

Answers (1)

Image Analyst
Image Analyst on 18 May 2015
Deanna:
You need to use the second (optional) return value of the max() function. That tells you the index where the maximum occurs . And of course just put starting and stopping indexes inside vec when you pass it to max() if you don't want it to examine the entire vector.
vec = [11 23 34 60 58 65 55 42 35 21]
[maxValue, indexOfMax] = max(vec(1:5)) % Find max in first 5 elements.

Community Treasure Hunt

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

Start Hunting!