Identifying identical vectors within a cell array and then grouping/moving them into a different cell arrays
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
BobbyRoberts
el 1 de Jul. de 2020
Comentada: BobbyRoberts
el 1 de Jul. de 2020
Hi. I have a cell array containing about 100 terms. Each term is a vector containing numbers from 1 : 6. Each vector could contain just 1 number or up to 6 numbers (each number is 1:6, but not ever 2 of the same number).
I'm trying to figure out a way to find terms containing identical vectors and group them into a separate cell array.
For example, say I have the following cell array:
{[5]; [1,2,3,5,6]; [1,2,3,5]; [3,5,6]; [1,2,3,4,5,6]; [3,5,6]; [3,4,6]; [5,6]; [1,2,3,6]; [5,6]; [5,6]; [5,6]; [1,2,3,4,5,6]}
I am trying to write code that would group identical terns. So, I would end up with 8 different groups (i.e. 8 different cell arrays) where each cell array containing contains x number of identical vectors.
EDIT: I realized what I need is the unique(x) function but for cell arrays of different lengths.
I hope this makes sense! Thanks a lot !
2 comentarios
Walter Roberson
el 1 de Jul. de 2020
{[5,6] ; [5,6]}
That is not a numeric vector like the others. Perhaps
{[5]; [1,2,3,5,6]; [1,2,3,5]; [3,5,6]; [1,2,3,4,5,6]; [3,5,6]; [3,4,6]; [5,6]; [1,2,3,6]; [5,6]; [5,6]; [5,6]; [1,2,3,4,5,6]}
Respuesta aceptada
Walter Roberson
el 1 de Jul. de 2020
data = {[5]; [1,2,3,5,6]; [1,2,3,5]; [3,5,6]; [1,2,3,4,5,6]; [3,5,6]; [3,4,6]; [5,6]; [1,2,3,6]; [5,6]; [5,6]; [5,6]; [1,2,3,4,5,6]};
[R,C] = ndgrid(1:length(data));
G = findgroups(sum(cumprod(arrayfun(@(r,c) ~isequal(data{r},data{c}), R, C),2),2));
Q = splitapply(@(v) {v} , data, G);
Q will be a cell array of cell arrays, each identical.
To me it does not make sense to hold on to all those duplicate copies. To me it would make more sense to
Q = splitapply(@(v) {v{1}, length(v)}, data, G)
This would be an N x 2 cell array in which the first column was the unique content, and the second column was the repeat count.
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrices and Arrays 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!