How can I remove identical cell entries containing small arrays

2 ビュー (過去 30 日間)
Alan Meier
Alan Meier 2020 年 4 月 6 日
コメント済み: Alan Meier 2020 年 4 月 8 日
I have a matlab cell. Each cell entry is containing a relatively small 2xn array of numbers. Some of these arrays are identical and therefor I want to remove the duplicated cell entry. I have found solutions in case I would have strings, string/characters, characters or numbers, but none for whole arrays inside a cell.
(Although shown here, the arrays are not always just twice in the cell.)
  3 件のコメント
Alan Meier
Alan Meier 2020 年 4 月 7 日
編集済み: dpb 2020 年 4 月 7 日
A_tmp = ; % cell with my data inside
A = {};
for i = 1:length(A_tmp)
if ismember(A_tmp{i}, A) == 0 %
A = [A; A_tmp{i}];
end
end
results in:
Error using cell/ismember (line 34)
Input A of class double and input B of class cell must be cell arrays of character vectors,
unless one is a character vector.
dpb
dpb 2020 年 4 月 7 日
if ismember(A_tmp{i}, A) == 0 %
A_tmp{} is the content of a cell while A is a cell (empty at first but still a cell). cell2mat exists to convert anyways. Not sure what you're trying to do here??? Why the catenation?
Also, ismember returns a logical array, not just a true|false so if content is more than a single element the return is a vector, not a value
all() might be of help here in that regards but I'd still look at converting the cells to arrays, and if want someone to actually try test code, providing them a sample dataset would likely induce a higher likelihood... :)

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2020 年 4 月 7 日
Two efficient loops, no third-party functions:
S = load('example.mat');
C = S.connect_group;
N = numel(C);
X = true(1,N);
for ii = 1:N
for jj = ii+1:N
if isequal(C{ii},C{jj})
X(ii) = false;
break
end
end
end
D = C(X);
  1 件のコメント
Alan Meier
Alan Meier 2020 年 4 月 8 日
This answer also has the advantage that it keeps the original order of the entries, which might be useful in some cases. The content of the cells only needs to be able to be processed by isequal's input restrictions, while my solution just works for everything one can create a hash from.

サインインしてコメントする。

その他の回答 (2 件)

Birdman
Birdman 2020 年 4 月 7 日
The following code should simply do the job for you:
unique(cell2mat(connect_group),'rows')
After this, you can regroup the arrays however you want.
  1 件のコメント
Alan Meier
Alan Meier 2020 年 4 月 7 日
It is resulting in an array, but the information is lost which of these rows belong together. I think I need to add some kind of marker for that. That's also the reason why I originally thought of keeping the data in the arrays inside the cell.

サインインしてコメントする。


Alan Meier
Alan Meier 2020 年 4 月 7 日
I found a solution, but I'm open to get pointed to better ones.
I hashed all cell entries using this function from file exchange.
hashes = cell(length(connect_group),1);
for i = 1:length(connect_group)
hashes{i} = DataHash(connect_group{i});
end
[~,idx]=unique(strcat(hashes(:,1)));
withoutduplicates = connect_group(idx,:);
The second part is based on this answer I mentioned above.

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by