从数组中删除非唯一行 [英] Deleting non-unique rows from an array

查看:86
本文介绍了从数组中删除非唯一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组a,如下所示:

I have an array a as follows:

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

我想删除在a中出现多次的所有行并获取c:

I want to delete all rows appearing more than once in a and get c:

c = [ 3 4 ];

请注意,这与保留唯一行不同,因为我根本不希望出现重复的行.我该怎么做?

Please note that this is not the same operation as keeping unique rows, since I don't want rows that had duplicates to appear at all. How can I accomplish this?

推荐答案

accumarray 一起使用,以计算出现次数,可以用来选择仅出现一次的行.

The third output of unique gives you the index of the unique row in the original array. You can use this with accumarray to count the number of occurrences, which can be used to select rows that occur only once.

例如:

A = [1 2; 3 4; 1 2];

[uniquerow, ~, rowidx] = unique(A, 'rows'); 
noccurrences = accumarray(rowidx, 1);

C = uniquerow(noccurrences==1, :);

哪个返回:

>> C

C =

     3     4

这篇关于从数组中删除非唯一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆