Finding maximum y value for the same x values and corresponding z values
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
sb
am 18 Jun. 2017
Bearbeitet: Andrei Bobrov
am 18 Jun. 2017
x y
1 5
1 7
1 9
2 8
2 22
2 11
5 5
5 10
5 20
I'd like to spit out an array of the max y coordinate that corresponds to each x coordinate. So I am using the following code provided in the community:
[uv,~,idx] = unique(xy(:,1)); %what do they correspond to?
xmax = accumarray(idx,xy(:,2),[],@max); %build matrix
[uv xmax]
My question is that if I have additional z column
z
1
9
7
8
6
5
3
2
4
then how can I also pick up the corresponding z values?
0 Kommentare
Akzeptierte Antwort
Star Strider
am 18 Jun. 2017
Use the ismember function with the 'rows' option:
xy = [1 5
1 7
1 9
2 8
2 22
2 11
5 5
5 10
5 20];
z = [1
9
7
8
6
5
3
2
4];
xyz = [xy z];
[uv,~,idx] = unique(xy(:,1)); %what do they correspond to?
xmax = accumarray(idx,xy(:,2),[],@max); %build matrix
Result = [uv xmax];
[~, ix] = ismember(Result(:,1:2), xyz(:,1:2), 'rows');
All_Columns = xyz(ix,:)
All_Columns =
1 9 7
2 22 6
5 20 4
0 Kommentare
Weitere Antworten (1)
Andrei Bobrov
am 18 Jun. 2017
Bearbeitet: Andrei Bobrov
am 18 Jun. 2017
T = table(x,y,z,'Va',{'x','y','z'});
out = varfun(@max,T,'gr','x');
or
z=[...
1
9
7
8
6
5
3
2
4];
xy = num2cell(...
[1 5
1 7
1 9
2 8
2 22
2 11
5 5
5 10
5 20],1);
[x,y] = xy{:};
xyz = [x,y,z];
[~,~,jj] = unique(x);
idx = accumarray(jj,(1:numel(x))',[],@(ii)ii(max(y(ii)) == y(ii)));
out = xyz(idx,:)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Other Formats finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!