M.N
M.N

Reputation: 343

how do i delete certain rows in a Matlab matrix?

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 1st 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!

Upvotes: 2

Views: 1197

Answers (1)

Jonas
Jonas

Reputation: 74940

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),:);

Upvotes: 5

Related Questions