Find repeated values of one column and assign the lowest value from the group of second columns

Hello, say I have the following matrix:
12709.2240000000 -1.23920000000000
12709.2240000000 -1.23530000000000
12780.2225000000 -1.23530000000000
12617.2233000000 -1.23530000000000
12564.2233000000 -1.23530000000000
12512.2233000000 -1.23530000000000
12709.2240000000 -1.23390000000000
12780.2225000000 -1.23090000000000
And I wanted to remove duplicate rows by looking at the 1st column, but assign the lowest value from the group of duplicated values to the 2nd column. So that I get the following:
12709.2240000000 -1.23920000000000
12780.2225000000 -1.23530000000000
12617.2233000000 -1.23530000000000
12564.2233000000 -1.23530000000000
12512.2233000000 -1.23530000000000
So in this case for example, I have 3 rows with 12709.224 on the 1st column, and assign -1.2392 to the 2nd column which is the lowest value from -1.2392, -1.2353, -1.2339.
I think a way to do this would be to find the index position of equal values on the 1st column and then compare the values of the 2nd column with this index position and assign these onto a new matrix, is there a better way / optimized to do this?
Thank you.

 採用された回答

M = [12709.2240000000 -1.23920000000000
12709.2240000000 -1.23530000000000
12780.2225000000 -1.23530000000000
12617.2233000000 -1.23530000000000
12564.2233000000 -1.23530000000000
12512.2233000000 -1.23530000000000
12709.2240000000 -1.23390000000000
12780.2225000000 -1.23090000000000];
[gid,~,g] = unique(M(:,1),'stable');
minM2 = splitapply(@min,M(:,2),g);
M_new = [gid,minM2];
format longG
disp(M_new);
12709.224 -1.2392 12780.2225 -1.2353 12617.2233 -1.2353 12564.2233 -1.2353 12512.2233 -1.2353

2 件のコメント

JIAN-HONG YE ZHU
JIAN-HONG YE ZHU 2023 年 2 月 21 日
移動済み: Voss 2023 年 2 月 22 日
Thank you very much
Voss
Voss 2023 年 2 月 22 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2017b

タグ

質問済み:

2023 年 2 月 19 日

コメント済み:

2023 年 2 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by