Find the maximum value in each group of a big matrix
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Jan
le 26 Avr 2019
Modifié(e) : Jan
le 26 Avr 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.
Réponse acceptée
Jan
le 26 Avr 2019
Modifié(e) : Jan
le 26 Avr 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 commentaires
Rik
le 26 Avr 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur NaNs dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!