Asked by Yaser Khojah
on 26 Apr 2019

I have a big matrix and I'm looking for an easy way to group them by a certain way then find the maximum in each group. I have created this code but I got stuck. Anyway to help please. I need to know the indexes of all the maximum points to extract more information from the big matrix.

data = randn(10,1)*10;

edges = 0:5:35';

Y = discretize(data,edges);

[B,I] = sort(Y);

SortedMatrix = [B data(I)];

Answer by Jan
on 26 Apr 2019

Edited by Jan
on 26 Apr 2019

Accepted Answer

data = randn(10,1)*10;

edges = [-Inf, 0:5:35, Inf];

Y = discretize(data, edges);

Result = accumarray(Y, data, [], @max)

I'm confused that this does not work reliably with splitapply:

Result = splitapply(@max, data, Y)

It fails with an error message, if an interval is empty:

For N groups, every integer between 1 and N must occur at least

once in the vector of group numbers.

Rik
on 26 Apr 2019

I was thinking way too complicated here. This was the start of my idea:

nanflag=false;

if any(isnan(B))

replaceNaN=max(B)+1;

B(isnan(B))=replaceNaN;

nanflag=true;

end

b=accumarray(B,Y,[],@max,NaN);

After which removal of empty lines (and putting back the NaN) to get to the desired output should be easy.

Yaser Khojah
on 26 Apr 2019

Dear Jan and Rik. Thank you for your help. I'm sorry if i made you both get confused

So, here what I want to do from what I asked earlier. I have this big file (MAT_All). I would like to create this thick line that shows the maximum at every x-axis. So, that is why I was looking for maximum for every interval but not sure if i will get this from what I asked earlier. Any idea?

scatter(MaT_All(:,18), MaT_All(:,17))

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Jan (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/458763-find-the-maximum-value-in-each-group-of-a-big-matrix#comment_698678

## Yaser Khojah (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/458763-find-the-maximum-value-in-each-group-of-a-big-matrix#comment_698683

## Yaser Khojah (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/458763-find-the-maximum-value-in-each-group-of-a-big-matrix#comment_698689

## Jan (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/458763-find-the-maximum-value-in-each-group-of-a-big-matrix#comment_698692

## Yaser Khojah (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/458763-find-the-maximum-value-in-each-group-of-a-big-matrix#comment_698698

Sign in to comment.