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