Finding unique cell elements
51 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
ayca
el 14 de Nov. de 2017
Comentada: majid huhu
el 12 de Jul. de 2021
I have a list as given below:
A= {[1 2], [2 1], [2 1 3],[3 4],[4 3]}
I need to simplify the matrix. For example, [3 4] and [4 3] form the same combination, only one of them is enough. Also, [1 2] and [2 1] is the same combinations, so I should be left with
newA= {[1 2],[2 1 3],[3 4]}
How do I do that?
0 comentarios
Respuesta aceptada
Stephen23
el 14 de Nov. de 2017
Editada: Stephen23
el 14 de Nov. de 2017
We can take advantage of the fact that unique accepts a cell array of char vectors, which means we can convert those numeric vectors to char and then the task is trivial, assuming that the order is not significant:
>> A = {[1,2],[2,1],[2,1,3],[3,4],[4,3]};
>> B = cellfun(@(v)sort(char(v)),A,'uni',0);
>> C = cellfun(@double,unique(B),'uni',0);
>> C{:}
ans =
1 2
ans =
1 2 3
ans =
3 4
1 comentario
majid huhu
el 12 de Jul. de 2021
If you got column vectors in cell array, you would probably get some like below error after running this solution.
Error using cell/unique>celluniqueR2012a (line 249)
Elements of cell array input A must be character vectors (row vectors of class char).
Error in cell/unique (line 89)
[varargout{1:nlhs}] = celluniqueR2012a(varargin{:});
Then you should convert all the items to row vectors and then perform such solution.
for i = 1 : numel(A)
A{i} = A{i}';
end
%% A = {[1; 2; 3], ...} --> A = {[1, 2, 3], ...}
>> A = {[1,2],[2,1],[2,1,3],[3,4],[4,3]};
>> B = cellfun(@(v)sort(char(v)),A,'uni',0);
>> C = cellfun(@double,unique(B),'uni',0);
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!