matlab 数组元素去重

1. 前言

今天老板突然在群里问了一个问题,

如何使用matlab 将一个数组中重复出现的所有元素剔除。 ie:
[1,1,2,3,4] ⇒ [2,3,4]

2. 实现思路

基本思路就是统计每个元素出现的次数, 然后选出出现次数为 1 次的进行输出。
使用C++ 我们可以借助一个 map 非常容易的实现, 但是要求用matlab 确实有些费劲。。。。

3. 实现方法

其中, histc 是用来统计向量中重复元素出现的次数
http://www.360doc.com/content/11/0111/11/1054746_85690513.shtml
基本功能实现了, 代码也蛮简洁的, 不过性能究竟怎么样没有测试过。。。
先做一个记录, 免得自己以后又忘了改怎么写~~

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% date          : 20161008
% version       : 1.0
% author        : zhyh2010
% description   : remove all the duplicate elements in a set
%                 for example:   [1,1,2,3,4]  ==> [2,3,4]
% input         : array to remove the duplicate elements
% ouput         : array already be removed the duplicated elements
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [output] = removeDuplicate(input)
    simple = unique(input);
    count = histc(input, simple);
    ids = find(count == 1);
    output = simple(ids);
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值