如何删除数组中的重复项但保持相同的顺序?

时间:2023-02-05 04:41:21

I have this cell array in MATLAB:

我在MATLAB中有这个单元格数组:

y = { 'd' 'f' 'a' 'g' 'g' 'a' 'w' 'h'}

I use unique(y) to get rid of the duplicates but it rearranges the strings in alphabetical order:

我使用unique(y)来删除重复项,但它按字母顺序重新排列字符串:

>> unique(y)

ans =

'a'    'd'    'f'    'g'    'h'    'w'

I want to remove the duplicates but keep the same order. I know I could write a function do do this but was wondering if there was a simpler way using unique to remove duplicates while keeping the same order just with the duplicates removed.

我想删除重复项但保持相同的顺序。我知道我可以编写一个函数来做这个但是想知道是否有更简单的方法使用unique来删除重复项,同时保持相同的顺序只删除重复项。

I want it to return this:

我希望它返回这个:

>> unique(y)

ans = 

'd'    'f'    'a'    'g'    'w'    'h'

3 个解决方案

#1


27  

Here's one solution that uses some additional input and output arguments that UNIQUE has:

这是一个使用UNIQUE具有的一些额外输入和输出参数的解决方案:

>> y = { 'd' 'f' 'a' 'g' 'g' 'a' 'w' 'h'};  %# Sample data
>> [~,index] = unique(y,'first');        %# Capture the index, ignore the actual values
>> y(sort(index))                           %# Index y with the sorted index

ans = 

    'd'    'f'    'a'    'g'    'w'    'h'

#2


21  

In MATLAB R2012a, a new order flag was added:

在MATLAB R2012a中,添加了一个新的订单标志:

>> y = {'d' 'f' 'a' 'g' 'g' 'a' 'w' 'h'};
>> unique(y, 'stable')
ans = 
    'd'    'f'    'a'    'g'    'w'    'h'

#3


6  

If you look at the documentation for unique, there's the option to return an index along with the sorted array. You can specify whether you want the first or last occurrence of a number to be returned to the index as well.

如果您查看文档的唯一性,可以选择返回索引以及排序的数组。您可以指定是否要将数字的第一个或最后一个匹配项返回到索引。

For example:

例如:

a=[5, 3, 4, 2, 1, 5, 4];

[b,order]=unique(a,'first')

returns

回报

b=[1, 2, 3, 4, 5] and m=[5, 4, 2, 3, 1]

b = [1,2,3,4,5]和m = [5,4,2,3,1]

You can sort your order array and store the index next

您可以对订单数组进行排序,然后存储索引

[~,index]=sort(order) %# use a throw-away variable instead of ~ for older versions

and finally re-index b

最后重新索引b

b=b(index)

#1


27  

Here's one solution that uses some additional input and output arguments that UNIQUE has:

这是一个使用UNIQUE具有的一些额外输入和输出参数的解决方案:

>> y = { 'd' 'f' 'a' 'g' 'g' 'a' 'w' 'h'};  %# Sample data
>> [~,index] = unique(y,'first');        %# Capture the index, ignore the actual values
>> y(sort(index))                           %# Index y with the sorted index

ans = 

    'd'    'f'    'a'    'g'    'w'    'h'

#2


21  

In MATLAB R2012a, a new order flag was added:

在MATLAB R2012a中,添加了一个新的订单标志:

>> y = {'d' 'f' 'a' 'g' 'g' 'a' 'w' 'h'};
>> unique(y, 'stable')
ans = 
    'd'    'f'    'a'    'g'    'w'    'h'

#3


6  

If you look at the documentation for unique, there's the option to return an index along with the sorted array. You can specify whether you want the first or last occurrence of a number to be returned to the index as well.

如果您查看文档的唯一性,可以选择返回索引以及排序的数组。您可以指定是否要将数字的第一个或最后一个匹配项返回到索引。

For example:

例如:

a=[5, 3, 4, 2, 1, 5, 4];

[b,order]=unique(a,'first')

returns

回报

b=[1, 2, 3, 4, 5] and m=[5, 4, 2, 3, 1]

b = [1,2,3,4,5]和m = [5,4,2,3,1]

You can sort your order array and store the index next

您可以对订单数组进行排序,然后存储索引

[~,index]=sort(order) %# use a throw-away variable instead of ~ for older versions

and finally re-index b

最后重新索引b

b=b(index)