Find values in list that appear exactly once
13 次查看(过去 30 天)
显示 更早的评论
Hi there,
assume, i have a list of integer values. I need a fast way to find those values in the list, that appear exactly once.
By now i have a code that finds the duplicate values and dismisses them.
list = randi(100,100,1); % Generate a list which should contain duplicate values
[vals,i]=sort(list);
j=find(all(diff(vals)==0,2));
ii=i([j;j+1]);
list(ii)=[];
Is there a faster way to get the final result?
Cheers, Johannes
0 个评论
采纳的回答
Stephen23
2017-8-25
编辑:Stephen23
2017-8-25
How about this:
[tmp,idx] = sort(list);
idp = diff(tmp)>0;
list(idx([true;idp]&[idp;true]))
If you are okay with getting the output sorted then it can be simplified further:
tmp = sort(list);
idp = diff(tmp)>0;
tmp([true;idp]&[idp;true])
3 个评论
Stephen23
2021-7-17
@Mohammadreza Mahmoodi: idp is a logical vector which indicates where the values change.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!