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

  • Thread starter Thread starter Keithwkc
  • Start date Start date
Click For Summary
The discussion focuses on a coding challenge involving the removal of outlier data points based on their x-coordinates relative to a defined curve. The goal is to replace x-coordinates that lie more than 2 units away from the curve with NaN values. The user provided an example with specific x and y coordinates for scattered points and a curve. Initial attempts using MATLAB code to identify and replace outliers were unsuccessful, particularly when multiple outliers shared the same y-axis value. A suggested solution involves calculating the absolute distances between the scattered points and the curve, identifying outliers based on a set threshold, and replacing those outliers with NaN values. The provided MATLAB code effectively demonstrates this approach, yielding the desired output. The discussion emphasizes the importance of using a distance-based criterion to filter out unwanted data points.
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.
 
Technology 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!
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 27 ·
Replies
27
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K