How to remove rows in a nested for loop?
2 views (last 30 days)
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) = ...
[Min_Distance, position] = min(Distance);
if Min_Distance < Threshold
TP = TP + 1;
Original_center(position,:) = ; %Remove row from matrix, as it is coupled
FP = FP + 1;
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.
Matt J on 20 Jan 2022
Edited: Matt J on 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,