Compare and remove entries from matrix

Hello all,
I have a question relating my code, since i'm doubting if it is very efficient. Suppose I have two matrices: (Not always integers)
A = [0 0 0; 0 0 1; 0 0 2; 0 0 3; 1 0 1; 1 0 2; 1 0 3; 1 1 1; 1 1 2; 1 1 3]
B = [0 0 2; 1 0 3; 1 1 1]
My goal is to compare matrices A and B, and remove/delete all entries in A that specify the following conditions:
  • First column value should be equal for A and B;
  • Second column value should be equal for A and B;
  • Third column value of A is larger than or equal to value of B + a numerical value;
  • Third column value of A is zero.
The code that I have written works as follows:
n = length(A);
while n>=1
Remove = A(:,1)==B(n,1) & A(:,2)==B(n,2) & A(:,3)>=(B(n,3)+VALUE) | A(:,3)==0;
A(Remove,:)=[]
n=n-1;
end
In reality matrix A can be easily over 1.000.000 rows in size, while the number of columns is always 3. This process can take several minutes for larger matrices. Therefore my question: can I make this process more efficient?

Answers (2)

James Tursa
James Tursa on 24 Feb 2017
Edited: James Tursa on 24 Feb 2017
Do NOT delete items from a matrix in a loop! Every time you do so, it causes an entire data copy to take place. If you do this a lot, it can easily dominate the run time. Instead, create a marker for each row you want removed, and then remove all of the rows at once. At the very least I think you would want to try something like this instead:
n = length(A);
A1 = A(:,1);
A2 = A(:,2);
A3 = A(:,3);
B3 = B(:,3) + VALUE;
Remove = A3==0;
for k=1:n
Remove = Remove | (A1==B(k,1) & A2==B(k,2) & A3>=B3(k));
end
A(Remove,:)=[];
Maybe even remove the A3==0 stuff prior to the loop ...

2 Comments

Thanks for the reply!
I've implemented your solution and found an performance improvement of roughly 25/30%. Any other ideas how I can skip searching through the large matrix A? Since this process will take the most time..
Do you have a supported C/C++ compiler available for building mex routines? This would be fairly easy to code up as a mex routine.

Sign in to comment.

Mark Kamps
Mark Kamps on 27 Feb 2017
Any other ideas on how I can improve this? To me it seems a rather inefficient way of removing entries from a matrix. Thanks in advance.

Categories

Asked:

on 24 Feb 2017

Commented:

on 27 Feb 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!