How Can I Remove Outlying Data Points from a Scatter Plot in MATLAB?

  • Thread starter Thread starter Keithwkc
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
Keithwkc
Messages
1
Reaction score
0
Greetings,

I have been trying to get rid of a bunch scattered data points whose x coordinates lie outside a distance from the x coordinates of a curve. These outlying x coordinates need to be changed into NaN's. The trouble is that a lot of these outlying points have the same y-axis values and my code has only been able to remove one of each set of data points with the same y-axis value.
For example:

scattered point x-coord= [1 2 3 4 5 6 7 8 9 10]

scattered point y-coord= [2 2 10 10 2 4 4 4 5 5 ]

curve x-coord= [0 1 2 3 4 5 6 7 8 9]

curve y-coord= [0 1 2 3 4 5 6 7 8 9]

Need to change elements of scattered point x-coord lying more than 2 units away from curve x-coord into NaN's, i.e.

desired scattered output x-coord= [1 2 NaN NaN NaN 6 NaN NaN NaN NaN]

Any advice on to obtain the above "desired scattered output x-coord" will be greatly appreciated.

I have tried using xi(find(abs(xi-xj)>=1))=NaN and even a for loop to run through each and every individual element

for i=1:n %n=number of elements in the vector scattered output coord
index=find(sx==cx(i)) % sx= scattered x-coord and cx= curve x-coord
sx(find(abs(sx(index)-cy(i))))=NaN % cy= curve y coord
end



but have not been successful in obtaining the desired result.

Thank you in advance for your help.
 
Physics news on Phys.org




Thank you for reaching out. It seems like you are trying to filter out data points that are too far away from a given curve. One way to approach this problem is to use a distance-based criteria to identify the outliers and then replace them with NaN's. Here is an example of how you can do this using MATLAB code:

% Define your data points
x = [1 2 3 4 5 6 7 8 9 10];
y = [2 2 10 10 2 4 4 4 5 5];

% Define your curve
curve_x = [0 1 2 3 4 5 6 7 8 9];
curve_y = [0 1 2 3 4 5 6 7 8 9];

% Set a distance threshold
threshold = 2;

% Calculate the distance between each data point and the curve
distances = abs(x - curve_x');

% Find the indices of the points that are more than the threshold distance away
outliers = find(distances > threshold);

% Replace the outliers with NaN's
x(outliers) = NaN;

% Print the result
disp(x);

This code will give you the desired output of [1 2 NaN NaN NaN 6 NaN NaN NaN NaN]. You can adjust the threshold value to fit your specific needs.

I hope this helps. Let me know if you have any further questions. Happy coding!