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
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
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 CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by