Identifying identical vectors within a cell array and then grouping/moving them into a different cell arrays
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2020-7-1
{[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]}
采纳的回答
Walter Roberson
2020-7-1
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!