How do you remove duplicates of nested cells?

2 ビュー (過去 30 日間)
Kris Govertsen
Kris Govertsen 2019 年 6 月 15 日
コメント済み: Kris Govertsen 2019 年 6 月 26 日
I have cell array that looks like the following:
RomanticGestures = ... % Cell array 5x1
{{List of Flowers};... % nested Cell array 15x1
{List of Chocolates};... % nested Cell array 15x1
{List of Movies};... % nested Cell array 17x1
{List of Flowers};... % nested Duplicate Cell array 15x1
{List of Wines}};... % nested Cell array 34x1
But I do not need the nested cell that contains the list of flowers twice. So this is what I want:
RomanticGestures = ... % Cell array 5x1
{{List of Flowers};... % nested Cell array 15x1
{List of Chocolates;... % nested Cell array 15x1
{List of Movies};... % nested Cell array 17x1
{List of Restaurants}}; % nested Cell array 34x1
I also need to know what row the duplicate occured in.
have tried unique but I think I have the notation wrong. I have tried:
RomanticGestures=unique(RomanticGestures)
Also
RomanticGestures=unique(cell2mat(RomanticGestures),'rows')
cell2mat doesnt work.
Any suggestions are appreciated :) Thank you!

採用された回答

Guillaume
Guillaume 2019 年 6 月 15 日
Sounds like:
Dates{1} = unique(Dates{1}); %What a misleading variable names if it doesn't contain dates/times!
is what you're after.
  3 件のコメント
Guillaume
Guillaume 2019 年 6 月 21 日
[uvalues, idxuvals] = unique(somecellarray);
idxduplicates = setdiff(1:numel(somecellarray), idxuvals);
will return the indices of all the elements that were removed by unique.
Kris Govertsen
Kris Govertsen 2019 年 6 月 26 日
This worked! I ended up doing:
[~, idxRomanticGestures] = unique(RomanticGestures);
RomanticGestures=RomanticGestures(idxRomanticGestures);
OtherRelatedArrays=OtherRelatedArrays(idxRomanticGestures);

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

その他の回答 (1 件)

Jan
Jan 2019 年 6 月 15 日
編集済み: Jan 2019 年 6 月 21 日
A loop approach:
C = RomanticGestures;
n = numel(C);
remove = false(1, n);
for i1 = 1:n
for i2 = i1 + 1:n
if ~remove(C{i2}) && isequal(C{i1}, C{i2})
remove(i2) = true;
end
end
end
C(remove) = [];
C = RomanticGestures;
n = numel(C);
H = cell(1, n);
for iC = 1:n
H{iC} = DataHash(C{iC}, 'base64');
end
[~, index] = unique(H);
Result = C(index)
  2 件のコメント
Kris Govertsen
Kris Govertsen 2019 年 6 月 20 日
DataHash doesnt work for characters
Jan
Jan 2019 年 6 月 21 日
編集済み: Jan 2019 年 6 月 21 日
@Kris: Please explain "doesn't work" with any details. I had a typo in my code, which is fixed now, but the problem was the round parentheses for indexing H, instead of the curly braces.
Of course DataHash works for characters.
If you provide some Matlab code, which produces the input data, I could test my code before posting it.

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

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by