Cell Array

Hello, is a simple question.
How can I remove repeated vectors from a cell array. Example:
Given a cell array {[1,2,3] [2,4] [1] [1,2,3] [5] [2,4] [1]}
I want the result:
{[1,2,3][2,4][1][5]} Thank you

回答(3 个)

Wayne King
Wayne King 2012-4-24
One way:
x = {[1,2,3] [2,4] [1] [1,2,3] [5] [2,4] [1]};
y = cellfun(@num2str,x,'uniformoutput',false);
xnew = unique(y);
xnew = cellfun(@str2num,xnew,'uniformoutput',false);
Andrei Bobrov
Andrei Bobrov 2012-4-24
y = cellfun(@(y)[y,zeros(1,max(cellfun('size',x,2)) - numel(y))],x,'un',0);
y = cat(1,y{:});
[a,b] = unique(y,'rows','first');
[ii,ii] = sort(b);
out = x(b(ii));
EDITED
[a,n,n] = unique([x{:}])
nn = cellfun('size',x,2);
y = mat2cell(n,1,nn);
y = cellfun(@(y)[y,k*ones(1,max(nn) - numel(y))],y,'un',0);
y = cat(1,y{:});
[a,b] = unique(y,'rows','first')
[ii,ii] = sort(b);
out = x(b(ii));

2 个评论

Jan
Jan 2012-4-24
Does this fail to process {[1], [1,0]}?
Hi Jan! Corrected.

请先登录,再进行评论。

Jan
Jan 2012-4-24
A dull FOR loop appraoch:
x = {[1,2,3] [2,4] [1] [1,2,3] [5] [2,4] [1]};
n = numel(x);
v = true(1, numel(x));
for i = 2:n
tmp = x{i};
for j = 1:i-1
if isequal(x{j}, tmp)
v(i) = false;
break; % Leave j loop
end
end
end
out = x(v);

类别

帮助中心File Exchange 中查找有关 Operators and Elementary Operations 的更多信息

提问:

2012-4-24

Community Treasure Hunt

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

Start Hunting!

Translated by