# How do I have matlab extract the max value of a column within a moving range?

9 views (last 30 days)
Katherine on 19 Dec 2016
Commented: Katherine on 20 Dec 2016
So this is a little complicated, but I need matlab to extract the max value of a column within a range that changes based on surrounding values. So for example if I had a column that looked like this:
Row number Data value
1 0.5
2 0.2
3 1
4 0.5
5 0.1
6 0.8
7 2
8 0.3
9 0.7
10 0.8
11 0.4
12 0.3
13 0.7
14 1
What I'd like a program to do is extract the largest value within 5 rows of that value. So, for example, the code should extract the 2 in row seven because it is within 5 rows of the 1 in row 3. It should also extract the 1 in row 14 because it's more than 5 rows away from the 2 in row 7.
I'm very new to matlab and I've been playing around with the few functions I do know, but I'm getting pretty stuck because I need this code to be interactive with the data set. I'd really appreciate any help/suggestions you might have.
Thank you!
Jan on 20 Dec 2016
Please post the input and the wanted output in valid Matlab syntax. Currently it is not clear how the output should look like.

Image Analyst on 19 Dec 2016
Use imdilate() if you have the Image Processing Toolbox. It's a moving max filter
kernel = [1;1;1;1;1]; % Vertical 5 by 1 filter.
result = imdilate(yourArray, kernel);
yourArray can be a 1 or 2 D array.
Katherine on 20 Dec 2016
Thank you! I believe this will work

Bjorn Gustavsson on 20 Dec 2016
If you have the image processing toolbox you have access to the function:
ordfilt2
as far as I understand your question you should get what you ask for:
x_maxin5 = ordfilt2(x,5,ones(5,1));
HTH
Image Analyst on 20 Dec 2016
Just to build on that: If the second argument is the length of the third argument, this is the same as imdilate(). With ordfilt2() you can specify which element you want so if the second argument is 1, it's the same as imerode() - a local sliding minimum. If it's the middle one, it's like a sliding median, medfilt2(). For other numbers in between, it's less intuitive what meaning they would have or where they'd be useful, or what effect they'd have on the image.

### Community Treasure Hunt

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

Start Hunting!