Unique values in cell array

72 ビュー (過去 30 日間)
Cside 2020 年 4 月 14 日
回答済み: Patrik Forssén 2021 年 10 月 2 日
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});
[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');
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 件のコメント
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)

Community Treasure Hunt

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

Start Hunting!

Translated by