Using unique fuction on cell array
조회 수: 3 (최근 30일)
이전 댓글 표시
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
답변 (1개)
dpb
2016년 1월 13일
>> 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
dpb
2016년 1월 14일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!