Delete Array Element Based on Conditions
10 views (last 30 days)
Show older comments
Fabio Taccaliti
on 15 Jul 2022
Commented: Fabio Taccaliti
on 15 Jul 2022
Hello,
I have an array Coordinates 3133x3 that contains the 3 coordinates (x,y,z) of a list of points. Column 1 is the x, column 2 the y and column 3 the z.
Inside this array I would like to remove some points (rows) besed on 2 different conditions:
1) Delete all points (so delete the corresponding rows in Coordinates) with z-coordinates that is more than +-5 aways from this reference z_ref array.
So for example if a point has z-coordinate 400 it will be deleted, contrary if it has z-ccordinate 410 or 412 it will be keeped. This to remove the outliers present.
z_ref = [411.2966 426.2966 441.2966 456.2966 471.2966 486.2966 501.2966 545 575 605 635 665 695];
2) If there are more points with the same z-coordinate (again in a range of +-5) keep just the one with the z-coordinate closer to z_ref. This the remove coinciding points and keeping just one.
for i = 1:length(Coordinates)
for j = z_ref
if Coordinates(i,3) >= j + 5 || Coordinates(i,3) <= j - 5
Coordinates(i,:) = [];
elseif Coordinates(i,3) <= Coordinates(i+1,3) + 5 || Coordinates(i,3) >= Coordinates(i+1,3) - 5
Coordinates(i+1,:) = [];
end
end
end
This is what I tried but it doesn't work.. Otherwise I was checking other methods like mask but still I was not able to make it works
Please help me, thanks in advance
0 Comments
Accepted Answer
Chunru
on 15 Jul 2022
Edited: Chunru
on 15 Jul 2022
load Coordinates.mat
z_ref = [411.2966 426.2966 441.2966 456.2966 471.2966 486.2966 501.2966 545 575 605 635 665 695];
for i = 1:length(Coordinates)
if min(abs(Coordinates(i,3) - z_ref)) >= 5
Coordinates(i,3) = nan;
end
end
Coordinates(isnan(Coordinates(:,3)), :) =[];
for i=1:length(z_ref)
within5 = find(abs(Coordinates(:, 3) - z_ref(i))<5);
[~, idx] = sort(abs(Coordinates(within5, 3) - z_ref(i)));
if length(idx)>1
Coordinates(within5(idx(2:end)), :) =[];
end
end
Coordinates
5 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!