How to get max of repeated values?

조회 수: 12 (최근 30일)
Conner Carriere
Conner Carriere 2022년 10월 24일
답변: John D'Errico 2022년 10월 24일
I have a matrix
C =
0.5000 0.5000 0.3000 1.0000 0.8000 0.3000 0.7000 0.7000 0.3000
0.4000 0.6000 0.8000 0.6000 0.8000 1.0000 0.8000 1.0000 1.2000
I am looking at the C(2,:) row, everytime there is a repeated instance, I need to take the values from C(1,instance) look at them and max them
The end matrix should look like this
D =
0.5000 1.0000 0.8000 0.7000 0.3000
0.4000 0.6000 0.8000 1.0000 1.2000
Trying to explain better
look at C(2,:)
only 1 value of 0.4, so max of it is 0.5
2 values of 0.6, these are .5 and 1.0, max of these is 1
3 values of 0.8, these are .3 .8 and .7, max of these is .8
so on so forth

채택된 답변

the cyclist
the cyclist 2022년 10월 24일
편집: the cyclist 2022년 10월 24일
Here is one way;
% Input
C = [0.5000 0.5000 0.3000 1.0000 0.8000 0.3000 0.7000 0.7000 0.3000
0.4000 0.6000 0.8000 0.6000 0.8000 1.0000 0.8000 1.0000 1.2000];
% Identify the unique values of the second row of C, along with the index to where
% each of those values appear
[uniqueC,~,indexFromUniqueCBackToAll] = unique(C(2,:));
% For convenience, define the number of unique values
numberUniqueC = numel(uniqueC);
% Preallocate the matrix where the max values are stored
maxRow1 = zeros(1,numberUniqueC);
% For each unique value, in order, find the max of the corresponding values
% in row 1
for nc = 1:numberUniqueC
indexToThisCValue = (indexFromUniqueCBackToAll==nc);
maxRow1(nc) = max(C(1,indexToThisCValue));
end
% Append the max values to the unique values to create the output
D = [maxRow1; uniqueC]
D = 2×5
0.5000 1.0000 0.8000 0.7000 0.3000 0.4000 0.6000 0.8000 1.0000 1.2000
  댓글 수: 1
Conner Carriere
Conner Carriere 2022년 10월 24일
Wow that works perfect! thanks for your quick input, now I am going to try and understand it.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Paul
Paul 2022년 10월 24일
Can use splitapply
C = [0.5000 0.5000 0.3000 1.0000 0.8000 0.3000 0.7000 0.7000 0.3000
0.4000 0.6000 0.8000 0.6000 0.8000 1.0000 0.8000 1.0000 1.2000];
[G,ID] = findgroups(C(2,:));
D = [splitapply(@max,C(1,:),G) ; ID]
D = 2×5
0.5000 1.0000 0.8000 0.7000 0.3000 0.4000 0.6000 0.8000 1.0000 1.2000

John D'Errico
John D'Errico 2022년 10월 24일
Easy enough. Use unique, then it is just a call to accumarray.
C = [0.5000 0.5000 0.3000 1.0000 0.8000 0.3000 0.7000 0.7000 0.3000
0.4000 0.6000 0.8000 0.6000 0.8000 1.0000 0.8000 1.0000 1.2000];
% First, match the second row with a set of indices. unique does this.
[C2unique,~,Uind] = unique(C(2,:));
% next, use accumarray to find the group maxima, for each repeated element,
% as identified by Uind
C1max = accumarray(Uind,C(1,:)',[numel(C2unique),1],@max);
Cfinal = [C1max';C2unique]
Cfinal = 2×5
0.5000 1.0000 0.8000 0.7000 0.3000 0.4000 0.6000 0.8000 1.0000 1.2000

카테고리

Help CenterFile Exchange에서 Fuzzy Logic Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by