How to remove rows in a nested for loop?

2 vues (au cours des 30 derniers jours)
S.
S. le 14 Jan 2022
Modifié(e) : S. le 13 Avr 2022
I have a matrix Detected_center and a matrix Original_center with in each row a x and y coordinate. I want to compare each time one row of the Detected_center matrix with all the rows in the Original_center matrix and calculate the distance. If the row of the Detected_center is paired with a row of the Original_center as the minimal distance is within a threshold, I want that this row is removed from the Original_center matrix for the sake of computational time.
Can any help me out how to do this in a save way? The beneath way is not save as the nested for loop should change in length.
Threshold = 0.2
for i = 1:length(Detected_center)
for j = 1:length(Original_center)
Distance(j,1) = ...
end
[Min_Distance, position] = min(Distance);
if Min_Distance < Threshold
TP = TP + 1;
Original_center(position,:) = []; %Remove row from matrix, as it is coupled
else
FP = FP + 1;
end
end
Blue center is from the matrix Detected_center, and the red centers are the centers of the matrix Original_center. The distances are calculated from the blue center to all red centers with pythagoras. If the distance between the red center which is the closest to the blue center falls within the threshold, it should be removed from the matrix Original_center.
  8 commentaires
Matt J
Matt J le 18 Jan 2022
This is because, the coordinates in Detected_center are given in four decimals precisely.
Not sure why that matters. My example data had infinite precision (they were integers). But if you think the issue can be ignored, see my answer below.
S.
S. le 18 Jan 2022
Well, it decreases the change that you get the same distances, if you have integers it's more likely you will get the exact same distances.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 20 Jan 2022
Modifié(e) : Matt J le 20 Jan 2022
It should start with the column (and therefore the Detected_center point) with the overall shortest distance to a Original_center point and then move on to the next Detected_center
This new processing rule can be implemented as follows,
Distances=pdist2(Original_center,Detected_center);
[M,N]=size(Distances);
discard=false(M,1);
while any(isfinite(Distances),'all')
[d,Rows]=min(Distances,[],1);
[dmin,col]=min(d);
if dmin<Threshold
row=Rows(col);
discard(row)=1;
Distances(row,:)=inf;
end
Distances(:,col)=inf;
end
Original_center(discard,:)=[];
TP=nnz(discard);
FP=M-TP;
  4 commentaires
S.
S. le 25 Jan 2022
Indeed!
S.
S. le 8 Avr 2022
Modifié(e) : S. le 13 Avr 2022
A small question about this. How would you write this is pseudocode to make it easy to understand for other readers? Do you have any ideas about that? I have some idea in mind aswell.

Connectez-vous pour commenter.

Plus de réponses (2)

Matt J
Matt J le 14 Jan 2022
Modifié(e) : Matt J le 14 Jan 2022
discard=ismembertol(Original_center,Detected_center,Threshold,'ByRows',1,'DataScale',1);
Original_center(discard,:)=[];
  7 commentaires
Matt J
Matt J le 18 Jan 2022
Well, I need to select the point of the matrix Original_center with the shortest distance to the point of the matrix Detected_center. See image in my original question to clarify this. So it's quite essential.
That doesn't explain why it needs to be the shortest Euclidean distance and not the shortest L-inf distance.
S.
S. le 18 Jan 2022
Well, the centers in Original_center are the actual coordinates of oranges in a picture and the Detected_center are the predicted coordinates of the oranges in the picture of the oranges. So for a robot arm to grab the orange, I think the Euclidean distance is necessary and most suitable. If you think something else would be more appropriate, feel free to mention.

Connectez-vous pour commenter.


Matt J
Matt J le 17 Jan 2022
For Euclidean distance,
discard=any( pdist2(Original_center,Detected_center)<Threshold ,2);
Original_center(discard,:)=[];
  8 commentaires
Matt J
Matt J le 18 Jan 2022
Modifié(e) : Matt J le 18 Jan 2022
Well, in this case you calculate all distances, which is not necessary and therefore computationally inefficient.
I don't know which version of my solution you're looking at. I return the shortest distance, just as you did. Regardless, I don't think using the 'smallest' flag makes pdist2 compute fewer distances. Output memory allocation is certainly more efficient when the flag is used, but pdist2 still needs to compute all the distances in order to know which one is the minimum.
tic
X=rand(1e6,2); Y=rand(1,2);
toc
Elapsed time is 0.026885 seconds.
tic;
D=pdist2(X,Y);
toc
Elapsed time is 0.468526 seconds.
tic
[D,I]=pdist2(X,Y,'euc','smallest',1);
toc
Elapsed time is 0.010583 seconds.
You can see that the difference in compute time above is basically the amount of time required to allocate memory for the result.
Moreover Original_center(loc(pos),:)=[]; is then not necessary. In the for loop I use, the one centerpoint that is coupled is removed from the Original_center matrix, so that the distance doesn't need to be calculated again.
It is much more efficient to remove the undesired points in one step, as I did, than to do it one at a time in a loop.
S.
S. le 20 Jan 2022
Modifié(e) : S. le 20 Jan 2022
The problem is, however, you calculate all the distances, while I calculate less distances, as if a pair is coupled the row is deleted. This is actually not the main issue, because there is now the following issue with the code. If you for example have the following distances with a threshold of 15:
If you use this code (my comment), the first Detected_center point will be coupled to the last row (first column), as that is the point with the minimal distance (8 pixels), while the last Detected_center point will NOT be coupled to any point as the first Detected_center point is already coupled to the last Detected_center point: 1 TP and 1 FP. --> So coupling seems not to be smart.
If you use this code (your comment), the first and last Detected_center point will be coupled to the last row (last Original center point), as it is for both the minimal distance: 2 TP
However, the correct way, is that the first Detected_center point is coupled to the second Original_center point, and the last Detected_center point will be coupled to the last Original_center point: 2 TP. How can I deal with/incorporate this? This is the only thing the software is sensitive to (I think). --> It should start with the column (and therefore the Detected_center point) with the overall shortest distance to a Original_center point and then move on to the next Detected_center point with the second overall shortest distance to a Original_center point etc.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by