Using unique fuction on cell array

7 views (last 30 days)
Stephan Richtering
Stephan Richtering on 13 Jan 2016
Commented: dpb on 14 Jan 2016
Hello,
This is following up on a previous post adding two cells together. http://www.mathworks.com/matlabcentral/answers/263633-combining-to-two-cells
The reason I this is because I couldn't use the unique function unless it was a cell array of string. So I changed the one column. However, I can't apply this to the whole table and I want to use the function X=unique(X,'stable'). So in the picture attached it would remove second of the two highlighted rows. The unique function doesn't work as there is a mix of cell types.
ALTERNATIVE:
Focus on three columns where one is a mixture of string/number, number and date (first three columns in the sample excel sheet).
For the string/number I want to use something similar to numstr(). From my previous question I probably create another column using the for loop
for n = 1:length(p)
A(n) = {[num2str(p{n,1}),k{n,1}]};
end
and then for the number I would use the suggestion from dpb using cellmat. For dates unique works fine.
Putting them together I would find the unique indices
Thanks, Stephan
  9 Comments
Stephan Richtering
Stephan Richtering on 14 Jan 2016
I would like to simplify the data! But I think the data is inputted by various sources and is not something that can be standardized. Many of them are a mixture of numbers and string in the format of 1,2...x 'ND'
Stephan Richtering
Stephan Richtering on 14 Jan 2016
Edited: Stephan Richtering on 14 Jan 2016
Edited the question as given your inputs it may be easier to focus on the data types I know. Not the exact solution but given the inconsistency in the data it probably it is the easiest to implement in Matlab

Sign in to comment.

Answers (1)

dpb
dpb on 13 Jan 2016
>> ccc % a sample cell array similar to shown...
ccc =
'13,14' [10700]
'13,14' [ 0]
'123' [ 200]
'123' [ 200]
>> [~,ia]=unique(cell2mat(ccc(:,2)),'stable') % get the unique indices from the 2nd column
ia =
1
2
3
>> ccc(ia,:) % show the result
ans =
'13,14' [10700]
'13,14' [ 0]
'123' [ 200]
>>
To pare the table simply reassign --
ccc=ccc(ia,:);
  4 Comments
Stephan Richtering
Stephan Richtering on 14 Jan 2016
Well the data structure may vary. It may be a string or a cell i.e. there are no restrictions of the input unless it is a date.
I can upload a data file but the data would not fully encompass all possibilities. I just want to simple remove duplicates as you can do in excel which doesn't differentiate between the different cell types (or maybe it does its own conversion)
dpb
dpb on 14 Jan 2016
I believe internally for that operation Excel does the comparison to each column individually behind the scenes and then combines those logical results. If your data really are so ill-formed as you say and you can't (or won't???) clean it up in the process of importing it to make it more manageable, then I'd posit the above is the only option you've left yourself.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!