Find the maximum value in each group of a big matrix

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)];

5 Comments

What does "group them by a certain way" mean? What exactly are "the indices of all the maximum points"? Please post a small example or define the procedure exactly.
So for example, my sortedMatrix is as below. Now The group is the first column and I would like to fix the max of each group (1,1,3 and 4). So Group 1 max is 3.7
1 0.773590911304249
1 3.71378812760058
3 11.1735613881447
4 15.3263030828475
NaN -8.63652821988714
NaN -12.1411704361541
NaN -11.1350074148676
NaN -0.0684932810334807
NaN -7.69665913753682
NaN -2.25584402271252
so I should have something the below and I want to know ther indices in the original from the original data
1 3.71378812760058
3 11.1735613881447
NaN -0.0684932810334807
Jan
Jan on 26 Apr 2019
Edited: Jan on 26 Apr 2019
So is sortedMatrix your actual input data? Or do you want to process data, which is set to a random array for a demonstration?
It would be easier, if you avoid to create the NaNs, e.g. by adding a final edge Inf. Then splitapply(@max, data(I), B) would solve the problem.
I have attached my data (Data) and I will explain what I'm trying to do on this file.
edges = 0:5:35';
% i want to split column 18 based on edges
Y = discretize(Data(:,18),edges);
% this is see the new data sorted
[B,I] = sort(Y);
SortedMatrix = [B Data(I,:)];
% How can i find the max in every edges. It might be there is more than a max but I want to know how they are related to the original data so I need their indexs in the original data

Sign in to comment.

 Accepted Answer

Jan
Jan on 26 Apr 2019
Edited: Jan on 26 Apr 2019
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.

2 Comments

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.
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))
Capture.JPG

Sign in to comment.

More Answers (0)

Tags

Asked:

on 26 Apr 2019

Commented:

on 26 Apr 2019

Community Treasure Hunt

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

Start Hunting!