从单元阵列中删除不同数据类型的重复值

时间:2016-12-20 13:21:37

标签: arrays matlab unique cell

我有一个包含许多重复值的单元格数组列表,其中包括字符串,采样时间,饱和度上限和下限。

例如:

MyValues={  'Lookc_at_the_stars'
            'Lookc_how_they_shine'
            'forc_you'
            'andm_everything_they_do'
            'Theym_were_all_yellow'
            'COLDPLAY_STOP'
            'COLDPLAY_PLAY'
            'COLDPLAY_PLAY'
            'COLDPLAY_PLAY'
            'COLDPLAY_BREAK'
            'COLDPLAY_BREAK'
            'Lookc_How_they_shinefor_you'
            'its_true'
            'COLDPLAY_STOP'
            'COLDPLAY_STOP'   }

我需要的输出是:

NewMyValues = { 'Lookc_at_the_stars'
                'Lookc_how_they_shine'
                'forc_you'
                'andm_everything_they_do'
                'Theym_were_all_yellow'
                'COLDPLAY_STOP'
                'COLDPLAY_PLAY'
                'COLDPLAY_BREAK'
                'Lookc_How_they_shinefor_you'
                'its_true'  }

由于我尝试使用函数unique,我无法得到输出,因为它给我一个错误,说

  

“使用cell / unique时出错   输入A必须是字符串的单元格数组。“

MyValues由不同类型的数据类型值组成。

有人可以提供解决方案或功能代码,我可以删除重复的值吗?

2 个答案:

答案 0 :(得分:0)

这是一个基于循环的解决方案,用于提取不同类型的单元格数组的唯一值:

MyValues={  'Lookc_at_the_stars',
            'Lookc_how_they_shine',
            1,
            'forc_you',
            'andm_everything_they_do',
            'Theym_were_all_yellow',
            2,
            'COLDPLAY_STOP',
            'COLDPLAY_PLAY',
            1,
            'COLDPLAY_PLAY',
            'COLDPLAY_PLAY',
            {1 2 3},
            'COLDPLAY_BREAK',
            {4 3 5},
            'COLDPLAY_BREAK',
            {1 2 3},
            'Lookc_How_they_shinefor_you',
            'its_true',
            'COLDPLAY_STOP',
            'COLDPLAY_STOP'   };
N = numel(MyValues);
idx = true(N,1);
for m = 1: N
    if idx(m)
        for n = (m+1):N
            if idx(n) && isequal(MyValues{m}, MyValues{n})
                idx(n) = false;
            end
        end
    end
end
result = MyValues(idx);

结果:

result =
{
  [1,1] = Lookc_at_the_stars
  [1,2] = Lookc_how_they_shine
  [1,3] =  1
  [1,4] = forc_you
  [1,5] = andm_everything_they_do
  [1,6] = Theym_were_all_yellow
  [1,7] =  2
  [1,8] = COLDPLAY_STOP
  [1,9] = COLDPLAY_PLAY
  [1,10] =
  {
    [1,1] =  1
    [1,2] =  2
    [1,3] =  3
  }
  [1,11] = COLDPLAY_BREAK
  [1,12] =
  {
    [1,1] =  4
    [1,2] =  3
    [1,3] =  5
  }
  [1,13] = Lookc_How_they_shinefor_you
  [1,14] = its_true
}

函数isequal可以比较使用它的所有值,比较所有值并删除重复项。所以result是一个包含唯一值的单元格数组 根据问题中的示例,如果要使用唯一的单元格数组,可以使用cellfunischar来检查单元格值是否为字符数组。然后使用逻辑索引提取它们并应用unique

unique(MyValues(cellfun(@ischar,MyValues)),'stable')

不使用stable选项,结果将被排序

答案 1 :(得分:0)

您在问题中提供的单元格仅包含字符串,因此unique可以处理它。但是,如果向其添加整数,浮点数或复数,则可以在调用唯一之前将所有单元格元素转换为字符串。例如,我会向您展示一个小strcaster函数

function y = strcaster(x)
    if ischar(x)
        y = x;
    elseif isreal(x)
        y = num2str(x);
    else
        if imag(x)>=0
            s = '+j';
        else
            s = '-j';
        end
        y = [num2str(real(x)),s,num2str(imag(x))];
    end
end

然后你可以获得独特的元素,保留它们在单元格中出现的顺序,执行以下操作:

MyValues={'Lookc_at_the_stars',...
'Lookc_how_they_shine',...
'forc_you',...
'andm_everything_they_do',...
'Theym_were_all_yellow',...
'COLDPLAY_STOP',...
'COLDPLAY_PLAY',...
'COLDPLAY_PLAY',...
'COLDPLAY_PLAY',...
'COLDPLAY_BREAK',...
'COLDPLAY_BREAK',...
'Lookc_How_they_shinefor_you',...
'its_true',...
'COLDPLAY_STOP',...
'COLDPLAY_STOP',...
1,...
1.32423,...
complex(-3.,13)};
[u,i] = unique(cellfun(@(x)strcaster(x),MyValues,'uniformoutput',false),'stable');
disp(MyValues(i))

修改

根据您的评论,很明显MyValues单元格包含其他单元格,您的问题的示例中并未明确这一点。获取MyValues中唯一值的最佳方法仍然是将内容转换为字符串。 JSON协议允许您将几乎任何数据类型转换为字符串,因此我建议以下列方式使用matlab的jsonencode函数:

MyValues={'Lookc_at_the_stars',...
'Lookc_how_they_shine',...
'forc_you',...
'andm_everything_they_do',...
'Theym_were_all_yellow',...
'COLDPLAY_STOP',...
'COLDPLAY_PLAY',...
'COLDPLAY_PLAY',...
'COLDPLAY_PLAY',...
'COLDPLAY_BREAK',...
'COLDPLAY_BREAK',...
'Lookc_How_they_shinefor_you',...
'its_true',...
'COLDPLAY_STOP',...
'COLDPLAY_STOP',...
1,...
1.32423,...
[1,23,4,4;5,3,2,1],...
{'bla',1324,{'12',123}},...
struct('NextTime','Provide a complete working example in the question')};
[u,i] = unique(cellfun(@(x)jsonencode(x),MyValues,'uniformoutput',false),'stable');
disp(MyValues(i))
相关问题