Newbie to Matlab: How to find duplicates within a cell of arrays, arrays have varying sizes

조회 수: 2 (최근 30일)
Hi,
I have a 100 x 1 cell containing arrays of varying length from 5 to 8. Here's an example:
A = [1x7 double] [1x6 double] [1x6 double] [1x7 double] [1x5 double]
Each of these arrays looks something like [1 5 20 12 25 27 1]. Is there a fast way to identify any duplicates within the entire cell and delete them so you're left with only unique arrays? I've been searching on this site but can't seem to find a way to deal with varying length arrays.
Thank you so much for your help!
Newbie to Matlab

채택된 답변

Teja Muppirala
Teja Muppirala 2011년 11월 15일
Here is a one rather exotic way of doing it:
% Step 1: Convert to strings
Aunique = cellfun(@(x)char(typecast(x,'uint8')), A, 'unif',0);
% Step 2: Call the UNIQUE command
Aunique = unique(Aunique);
% Step 3: Reconvert to doubles
Aunique = cellfun(@(x) typecast(uint8(x),'double'),Aunique,'unif',0)
  댓글 수: 2
andrew
andrew 2011년 11월 15일
Okay, as an extension to this problem, if I have another 100 x 1 matrix that is related to my first cell (ex. this second cell would be the values associated with the first cell), then is there a good way using what you've done to ensure I delete the row in the second cell at the same time I delete the row in the first?
For example, if I have:
A = [1x7 double] [1x6 double] [1x6 double] [1x7 double] [1x5 double]
and values = [75; 55; 50; 45; 75]
When I delete the [1x7] in A assuming these are the same, how can I make sure I delete the corresponding 75 in the values array

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 11월 15일
A( any(triu(bsxfun(@isequal, A, A.'),1)) ) = [];
  댓글 수: 3
Walter Roberson
Walter Roberson 2011년 11월 15일
There might be a version difference: the current documentation does indicate that cells are allowed.
You could use
A( any(triu(0+bsxfun(@(J,K) isequal(A{J},A{K}), (1:length(A)).', 1:length(A)),1)) ) = [];
andrew
andrew 2011년 11월 15일
Thanks again, now I'm getting the error 'invalid output dimensions' maybe I'm using an old version?

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

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by