从嵌套元胞数组中删除重复的字符串

Removing duplicate strings from a nested cell array

我有一个名为 tmp 的 1x234 元胞数组,我想从中删除所有重复项。 tmp 的一些元素可以是:

tmp{1, 3} -> 'Ro'
tmp{1, 167}{1, 1} -> 'Ro'
tmp{1, 167}{1, 2} -> 'CvF'

在前两个中,我只想保留 tmp{1, 167}{1, 1} -> 'Ro'

此外,我想转换包含单元格内单元格的条目,例如:

tmp{1, 167}{1, 1} -> 'Ro'
tmp{1, 167}{1, 2} -> 'CvF'

进入:

tmp{1, 167}-> 'Ro'
tmp{1, 168}-> 'CvF'

我怎样才能实现这两个目标?

TL;DR: unq = unique([tmp{:}]);


如果您的最大嵌套级别为 2(即单元格中的单元格),则展平数组不需要任何类型的递归函数,并且可以使用简单的串联来实现。请参阅下面的完整示例:

function unq = q66221704()
%% Generate some data:
rng(66221704); % for reproducibility
N = 2;
tmp = cell(1,234);
for k = 1:numel(tmp)
  if rand(1) < 0.6 % create nested cells with some probability
    tmp{k} = getLowercaseString(N);
  else
    tmp{k} = {getLowercaseString(N), getLowercaseString(N)};
  end
end

%% Flatten cell array:
new = [tmp{:}]; % size = 1x352

%% Keep uniques:
unq = unique(new); % size = 1x259
end

function c = getLowercaseString(nChars)
c = char(randi([97, 122], [1, nChars]));
end