Delete Array Element Based on Conditions

10 views (last 30 days)
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

Accepted Answer

Chunru
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
Coordinates = 11×3
95.0460 -7.8772 543.5237 34.2147 -8.4905 426.8342 64.3050 -3.5719 441.3390 34.6908 -9.2050 457.0454 96.6137 -28.8350 631.4779 95.4812 -14.4214 573.1167 96.0615 -21.6073 602.2293 96.7264 -35.9699 660.8084 93.3768 3.1698 471.1664 93.9380 2.5895 501.2340
  5 Comments

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!