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!