开发者

how do i delete certain rows in a Matlab matrix?

开发者 https://www.devze.com 2023-02-19 01:14 出处:网络
I have a matrix of doubles with 4892 rows and 4 columns. Say I have N rows with the same values in the 3rd and 4th columns (but not necessarily in the 1s开发者_Python百科t and 2nd columns), I would l

I have a matrix of doubles with 4892 rows and 4 columns.

Say I have N rows with the same values in the 3rd and 4th columns (but not necessarily in the 1s开发者_Python百科t and 2nd columns), I would like to leave only one row out of the group.

An example:

1738 1738 8611 8611

1739 1738 8611 8611

1739 1739 8611 8611

I would like to leave only one row out of this bunch (doesn't matter which one).

How do I do this?

Thanks!


Use UNIQUE. By default, this will keep the last row.

%# array is your 4892-by-4 array
%# call 'unique(array(:,3:4),'rows','first') if you want to keep the first row
[~,idx] = unique(array(:,3:4),'rows');

%# use sort if you want to preserve the original order of rows
trimmedArray = array(sort(idx),:);
0

精彩评论

暂无评论...
验证码 换一张
取 消