unique across multiple cells or arrays
Mostrar comentarios más antiguos
Let's say I got a bunch of arrays in a cell array that are not the same size:
a = {[1 2 2], [2 3 6 3], [4 3 5 6 7]}
I want to remove all dupplicates accross the cells. I want to remove things in a cell that are in the other cells as well as the dupplicates in each cells:
Example output:
b = {[1 2], [3 6], [4 5 7]}
I would like to have this vectorized. I can simply loop but this is very slow:
others = [];
for i = 1:length(a)
b{i} = setdiff(unique(a{i}), others);
others = [others, b{i}];
end
I'm thinking of putting it all into a single array to call unique:
b = unique([a{:}])
% now b = [1 2 3 4 5 6 7]
But I don't think there's anyway to put it back into its respective cells since the order is lost and I don't know where the limits are
4 comentarios
per isakson
el 7 de Ag. de 2020
Editada: per isakson
el 7 de Ag. de 2020
Read the documentation on unique and start with something like this
len = cellfun( @numel, a ); % remember the origins of the elements
[ C, ixa, ixc ] = unique( [a{:}], 'stable' );
Next an indexing excercise will do it - I think.
Alexander Winter
el 7 de Ag. de 2020
Editada: Alexander Winter
el 7 de Ag. de 2020
per isakson
el 8 de Ag. de 2020
Thanks for sharing. I cannot think of anything simpler.
Matt J
el 8 de Ag. de 2020
The process you describe does not have a well defined output. For the example you gave,
a = {[1 2 2], [2 3 6 3], [4 3 5 6 7]}
this would also satisfy the requirements:
b = {[1], [2 3], [4 5 6 7]}
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!