Compare and remove entries from matrix
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
0 Kommentare
Antworten (2)
James Tursa
am 24 Feb. 2017
Bearbeitet: James Tursa
am 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 Kommentare
James Tursa
am 27 Feb. 2017
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.
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!