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

1 Ansicht (letzte 30 Tage)
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!

Akzeptierte Antwort

Jonathan Cheng
Jonathan Cheng am 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

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by