How to find and combine identical numeric arrays within cell arrays?

1 view (last 30 days)
If I have an array with a combination of scalars and different sized vectors within a cell A: A{1} = 10; A{2} = [5 2]; A{3} = [3 9 2]; A{4} = 1; A{5} = [3 9 2]; A{6} = [5 2]; How do I find and merge identical vectors in the order of their first appearance, such that I get: B{1} = 10; B{2} = [5 2]; B{3} = [3 9 2]; B{4} = 1;
I've tried unique and union but it didn't seem to work on numeric arrays. Thanks!

Accepted Answer

Jonathan Cheng
Jonathan Cheng on 27 Apr 2016
I think I may have found the answer to my own question here, from Mat Fig in the comments section at http://www.mathworks.com/matlabcentral/fileexchange/25917-unique-rows-for-a-cell-array
ii = 1;
while ii<size(A,1)
tf = true(size(A,1),1);
for jj = ii:size(A,1)
if isequal(A(ii,:),A(jj,:)) && ii~=jj
tf(jj) = 0;
end
end
A = A(tf,:);
ii = ii + 1;
end

More Answers (0)

Categories

Find more on Matrices and Arrays 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!