Unique rows of 2 cells with 2 columns each.
Show older comments
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 Comments
madhan ravi
on 13 Sep 2019
Mind sharing the data as .mat file??
BdS
on 17 Sep 2019
Guillaume
on 17 Sep 2019
Is the question resolved or not? You've accepted an answer but seem to still be asking something. if you're still asking something, what do you want as an output? A 1886x2 cell array whose first column is either 1 or 2 (n. of repeat) and 2nd column is the matching value from column2 of either input?
BdS
on 17 Sep 2019
Guillaume
on 17 Sep 2019
I understand "containing the unique values" as containing for example:
column1 = {'1 HK Equity';
'10 HK Equity';
'101 HK Equity';
'1038 HK Equity';
'11 HK Equity';
...
};
I understand "nr. unique value" as the count of each unique value, so:
column1 = [2;
2;
1;
2;
...
];
so I'm confused as to what you want.
BdS
on 17 Sep 2019
BdS
on 17 Sep 2019
Accepted Answer
More Answers (1)
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 Comments
BdS
on 17 Sep 2019
Guillaume
on 17 Sep 2019
The 'rows' option of unique does not work with cell arrays. It gives you a warning if you try to do it (in my opinion, it should throw an error):
>> result = unique([new; old], 'rows');
Warning: The 'rows' input is not supported for cell array inputs.
> In cell/unique>celluniqueR2012a (line 236)
In cell/unique (line 148)
However, the 'rows' option does work with string arrays, so another option would be to convert your cell arrays of char vectors into string arrays (requires R2016b or later):
result = unique(string([new; old]), 'rows');
You can then keep working with the string array or if you really want to convert back to cell array:
resultcell = cellstr(result);
BdS
on 18 Sep 2019
Guillaume
on 18 Sep 2019
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.
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!