Unique rows of 2 cells with 2 columns each.

2 ビュー (過去 30 日間)
BdS
BdS 2019 年 9 月 13 日
コメント済み: Guillaume 2019 年 9 月 18 日
Hi
I have got 2 cells with 2 columns each:
IdxMembers(1636x2)
IdxMembers2(1630x2)
I am looking of the unique values of each cells in form of (nr. of unique values in first row x 2 columns)
I tried the following 2 codes:
unique(vertcat(IdxMembers,IdxMembers2),'rows')
and
unique(vertcat(IdxMembers,IdxMembers2))
I get the unique values in dim (nr. of unique values x 1 column). However, I would like get the output with the second column.
Do you have any suggestions?
  7 件のコメント
BdS
BdS 2019 年 9 月 17 日
I am sorry for the confusion.
I mean as your wrote in your first example. But with the corresponding value in column 1
Thanks
BdS
BdS 2019 年 9 月 17 日
The unique values should be based on the information based on column 1

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

採用された回答

Matt J
Matt J 2019 年 9 月 13 日
I tried the following 2 codes: unique(vertcat(IdxMembers,IdxMembers2),'rows')... I get the unique values in dim (nr. of unique values x 1 column).
No, you really should have gotten 2 column output, as in the example below. Something about your situation must be different from what you are describing.
>> [IdxMembers,IdxMembers2] =deal( randi(3,4,2) , randi(3,3,2))
IdxMembers =
2 1
2 2
3 2
3 2
IdxMembers2 =
3 3
3 2
1 1
>> unique(vertcat(IdxMembers,IdxMembers2),'rows')
ans =
1 1
2 1
2 2
3 2
3 3

その他の回答 (1 件)

Guillaume
Guillaume 2019 年 9 月 17 日
編集済み: Guillaume 2019 年 9 月 17 日
merged = [new; old]; %simpler way to write vertcat(new, old)
[~, row] = unique(merged(:, 1)); %get row index of unique values in the 1st column
result = merged(row, :) %extract these rows
  4 件のコメント
BdS
BdS 2019 年 9 月 18 日
thank you for your explanation.
Option 1 seems to more efficient: 0.007129 seconds in comparison with 0.028908
Guillaume
Guillaume 2019 年 9 月 18 日
Option 1 seems to more efficient: 0.007129 seconds
As long as you consider efficiency to be just the execution speed and that you care about a few milliseconds difference.
Indeed string arrays may have a slight speed impact but if you consider that if you'd used string arrays to start with your original code would have worked straight away but instead it took you 4 days to resolve the problem, in term of development time string arrays would have won hands down.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by