What's the most efficient/elegant way to delete elements from a matrix in MATLAB?

Kamran Bigdely picture Kamran Bigdely · Mar 25, 2009 · Viewed 31.3k times · Source

I want to delete several specific values from a matrix (if they exist). It is highly probable that there are multiple copies of the values in the matrix.

For example, consider an N-by-2 matrix intersections. If the pairs of values [a b] and [c d] exist as rows in that matrix, I want to delete them.

Let's say I want to delete rows like [-2.0 0.5] and [7 7] in the following matrix:

intersections =

   -4.0000    0.5000
   -2.0000    0.5000
    2.0000    3.0000
    4.0000    0.5000
   -2.0000    0.5000

So that after deletion I get:

intersections = 

   -4.0000    0.5000
    2.0000    3.0000
    4.0000    0.5000

What's the most efficient/elegant way to do this?

Answer

gnovice picture gnovice · Mar 25, 2009

Try this one-liner (where A is your intersection matrix and B is the value to remove):

A = [-4.0 0.5;
     -2.0 0.5;
      2.0 3.0;
      4.0 0.5;
     -2.0 0.5];
B = [-2.0 0.5];
A = A(~all(A == repmat(B,size(A,1),1),2),:);

Then just repeat the last line for each new B you want to remove.

EDIT:

...and here's another option:

A = A((A(:,1) ~= B(1)) | (A(:,2) ~= B(2)),:);

WARNING: The answers here are best used for cases where small floating point errors are not expected (i.e. with integer values). As noted in this follow-up question, using the "==" and "~=" operators can cause unwanted results. In such cases, the above options should be modified to use relational operators instead of equality operators. For example, the second option I added would be changed to:

tolerance = 0.001;   % Or whatever limit you want to set
A = A((abs(A(:,1)-B(1)) > tolerance) | (abs(A(:,2)-B(2)) > tolerance),:);

Just a quick head's up! =)


SOME RUDIMENTARY TIMING:

In case anyone was really interested in efficiency, I just did some simple timing for three different ways to get the subindex for the matrix (the two options I've listed above and Fanfan's STRMATCH option):

>> % Timing for option #1 indexing:
>> tic; for i=1:10000, index = ~all(A == repmat(B,size(A,1),1),2); end; toc;
Elapsed time is 0.262648 seconds.
>> % Timing for option #2 indexing:
>> tic; for i=1:10000, index = (A(:,1) ~= B(1)) | (A(:,2) ~= B(2)); end; toc;
Elapsed time is 0.100858 seconds.
>> % Timing for STRMATCH indexing:
>> tic; for i=1:10000, index = strmatch(B,A); end; toc;
Elapsed time is 0.192306 seconds.

As you can see, the STRMATCH option is faster than my first suggestion, but my second suggestion is the fastest of all three. Note however that my options and Fanfan's do slightly different things: my options return logical indices of the rows to keep, and Fanfan's returns linear indices of the rows to remove. That's why the STRMATCH option uses the form:

A(index,:) = [];

while mine use the form:

A = A(index,:);

However, my indices can be negated to use the first form (indexing rows to remove):

A(all(A == repmat(B,size(A,1),1),2),:) = [];    % For option #1
A((A(:,1) == B(1)) & (A(:,2) == B(2)),:) = [];  % For option #2