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
SUMMARY

This discussion addresses the removal of outlying data points from a scatter plot in MATLAB by replacing specific x-coordinates with NaN values based on their distance from a defined curve. The user seeks to filter out points whose x-coordinates are more than 2 units away from the curve's x-coordinates. A provided MATLAB code snippet demonstrates how to calculate distances and identify outliers effectively, resulting in the desired output of [1 2 NaN NaN NaN 6 NaN NaN NaN NaN]. This solution utilizes distance-based criteria to achieve the filtering.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of scatter plots and data visualization
  • Knowledge of distance calculations in numerical arrays
  • Basic experience with handling NaN values in datasets
NEXT STEPS
  • Explore MATLAB's array manipulation functions for advanced data filtering
  • Learn about MATLAB's logical indexing to enhance data processing techniques
  • Investigate the use of interpolation methods for handling NaN values in datasets
  • Study additional data cleaning techniques for outlier detection in MATLAB
USEFUL FOR

Data analysts, MATLAB users, and researchers involved in data visualization and preprocessing who need to manage outliers in scatter plots effectively.

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!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · 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
4K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K