Retain IDs of rows kept by the Unique function

3 ビュー (過去 30 日間)
Alessandro
Alessandro 2017 年 3 月 1 日
編集済み: Alessandro 2017 年 3 月 1 日
I have a matrix composed in the following way: [ID_1 ID_2 Resisting_Area]
ID_1 and ID_2 are integer values, Resisting_Area is float.
I need to perform the unique operation on the first 2 columns of the matrix, but also I need to carry the third column value for the kept rows. Is there a simple way to do this?
MWE:
A = [1 2 3.0001; 1 2 3.0002; 2 4 5.12];
I would like to select only the first and third rows but
B = unique(A,'rows')
won't do it, since the resisting area value is not integer and it is not correctly detected as duplicate. Also uniquetol does not have a 'rows' option.

採用された回答

Roger Stafford
Roger Stafford 2017 年 3 月 1 日
編集済み: Roger Stafford 2017 年 3 月 1 日
[U,m] = unique(A(:,1:2),rows);
B = [U,m];
I'm not sure which you want to keep, the indices of the "kept" rows or those kept third column values themselves. If the latter, change the second line of code to:
B = [U,A(m,3)];
Note that the rows that are "kept" depends on the 'first' or 'last' option, though that doesn't affect the contents of U, only that of m.
  1 件のコメント
Alessandro
Alessandro 2017 年 3 月 1 日
編集済み: Alessandro 2017 年 3 月 1 日
That's exactly what I was looking for. For some reason I didn't understand the second output argument was used to track IDs of the sorted first output, when reading the help. Plus, I do not really care which one is kept when a repetition is found. The 3rd column values only differ for floating errors. Thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by