Unique values in cell array

조회 수: 210 (최근 30일)
Cside
Cside 2020년 4월 14일
댓글: Madmad 2024년 3월 25일
Hi, I have a cell array and would like to find the unique values (by row) in the cell array. Each row has a variable number of numbers. However, in the documentation, unique function does not support cell array by row. Is there a way i can get past this? Thanks!
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2020년 4월 14일
Charms - do you mean that you want to find the unique integers from the two columns in a single row?

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

채택된 답변

Star Strider
Star Strider 2020년 4월 14일
The cells in ‘locations’ have from 3 to 10 columns, so comparing all of them by rows is not possible.
Likely the best you can do is to get the unique rows of the elements in ‘locations’ that share the same number of columns.
This code does that:
D = load('locations.mat');
locations = D.locations;
for k = 1:size(locations,1)
rowc{k,:} = [locations{k,:}];
rowlen(k,:) = numel(rowc{k});
end
[lenv,~,ix] = unique(rowlen);
eqcol = accumarray(ix, (1:numel(rowlen))', [], @(x){x});
for k = 1:numel(lenv)
rowm = cell2mat(rowc(eqcol{k}));
U{k,:} = unique(rowm, 'rows');
end
To see the result, for example for the 4-column cells:
Rows4 = cell2mat(rowc(eqcol{2}))
Check4 = U{2}
with ‘Rows4’ being the original cell array of those combined cells, and ‘Check4’ being the unique rows.
.
  댓글 수: 2
Dave
Dave 2020년 4월 14일
Maybe I misunderstand but doesn't the following meet your requirements
B = cellfun(B = cellfun(@unique, locations(:), 'UniformOutput', false))
Star Strider
Star Strider 2020년 4월 14일
@Dave — First, that is definitely incorrect MATLAB code. It is not possible to run it.
Second, Charms wants to see if the rows are unique, that being the whole point.
So I seriously doubt that it would.

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

추가 답변 (1개)

Patrik Forssén
Patrik Forssén 2021년 10월 2일
My new FEX-submission uniquearray,
solves this (and works for any type of array with any number of dimensions). Just use,
load('locations.mat', 'locations');
uniqueloc = uniquearray(locations)
  댓글 수: 1
Madmad
Madmad 2024년 3월 25일
Thanks for this script it helped me, I just had to divide my 1x200,000 cell into smaller steps to speed up the process, I think your script check the entire variable, which made it slow for my var, but in the end it worked!

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by