필터 지우기
필터 지우기

Identifying identical vectors within a cell array and then grouping/moving them into a different cell arrays

조회 수: 3 (최근 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]}
BobbyRoberts
BobbyRoberts 2020년 7월 1일
편집: BobbyRoberts 2020년 7월 1일
thanks for catching that.. That's what I meant to type. I have changed question to reflect that.

댓글을 달려면 로그인하십시오.

채택된 답변

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