Removing certain values from columns in a matrix

1 回表示 (過去 30 日間)
Holmbrero
Holmbrero 2020 年 11 月 25 日
コメント済み: Ameer Hamza 2020 年 11 月 25 日
Hi!
I have a matrix looking something like this:
21 4.51076
21 48.3399
19 11.4743
21 36.6765
22 18.3587
23 19.7070
21 59.0842
20 40.9994
24 4.93227
28 44.4808
25 18.9996
24 29.4205
26 17.5263
26 28.4772
The elements in column 1 are as you can see not unique.
Out of the entrys in column 1 with the same value, i would like to keep only the smallest value in column 2 such as:
21 4.51076
NaN NaN
19 11.4743
Nan NaN
22 18.3587
23 19.7070
Nan NaN
20 40.9994
24 4.93227
28 44.4808
25 18.9996
NaN NaN
26 17.5263
NaN NaN
Any suggestions?
Regards,
Anders Holmberg

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 11 月 25 日
編集済み: Ameer Hamza 2020 年 11 月 25 日
Try this
A = [
21 4.51076
21 48.3399
19 11.4743
21 36.6765
22 18.3587
23 19.7070
21 59.0842
20 40.9994
24 4.93227
28 44.4808
25 18.9996
24 29.4205
26 17.5263
26 28.4772];
[~, idx] = sort(A(:,1));
C = splitapply(@(x) {[x(1,1) min(x(:,2)); nan(size(x)-[1 0])]}, A, findgroups(A(:,1)));
M = cell2mat(C);
M(idx,:) = M;
Result
>> M
M =
21.0000 4.5108
NaN NaN
19.0000 11.4743
NaN NaN
22.0000 18.3587
23.0000 19.7070
NaN NaN
20.0000 40.9994
24.0000 4.9323
28.0000 44.4808
25.0000 18.9996
NaN NaN
26.0000 17.5263
NaN NaN
  2 件のコメント
Holmbrero
Holmbrero 2020 年 11 月 25 日
That works perfectly! Thank you for the fast reply and solution.
Regards,
Anders Holmberg
Ameer Hamza
Ameer Hamza 2020 年 11 月 25 日
I am glad to be of help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by