字符串数组matlab中的唯一条目

时间:2014-01-15 15:50:34

标签: arrays matlab unique

我有一个单元格数组,其中每个单元格都是以下形式的字符串:

A{1} = hello, world, the, hello, the, how, are, you, world

我想只保留唯一的单词,给我一个新的char数组,它将进入输出单元格数组:

B{1} = hello, world, the, how, are, you

unique()似乎没有削减它。这样做有简单的方法吗?

3 个答案:

答案 0 :(得分:3)

我同意之前的一些评论,质疑为什么数据采用这种格式(如果您有选择)。

然而:

>> A{1} = 'hello, world, the, hello, the, how, are, you, world';
>> B{1} = strjoin(unique(strtrim(strsplit(A{1}, ','))), ', ')

B = 

    'are, hello, how, the, world, you'

希望有所帮助。

*编辑* 另一种解决方案,如果您知道总会有空格,则删除strtrim。此外,如果您不希望操作对字符串进行排序,则可以使用对“唯一”的“稳定”调用:

>> B{1} = strjoin(unique(strsplit(A{1}, ', '),'stable'), ', ')

B = 

    'hello, world, the, how, are, you'

答案 1 :(得分:2)

我怀疑A {1}是一个长字符串。

A{1} = 'hello, world, the, hello, the, how, are, you, world';

在这种情况下,unique将只返回一个或多个字符串。

>> unique(A)
ans = 
    'hello, world, the, hello, the, how, are, you, world'

>> unique(A{:})
ans =
    ,adehlortuwy.

首先需要将字符串转换为单元格数组。

>> a = textscan(A{1},'%s','delimiter',',')
a = 
    {9x1 cell}

>> b = unique(a{:})
b = 
    'are'
    'hello'
    'how'
    'the'
    'world'
    'you'

然后如果你想再次转换回长字符串

>> sprintf('%s,',b{:})
ans =
    are,hello,how,the,world,you,

虽然说实话,如果我必须以这种方式处理字符串,我不会在一个长字符串中用它们开始。

答案 2 :(得分:0)

您可能没有正确使用unique()。它应该运作良好。见例:

A = {'one','two','twenty-two','One','two'};
C = unique(A)

你会得到

C = 

    'One'    'one'    'twenty-two'    'two'