How to draw a line of best fit on filtered discrete data sets
Show older comments
How can i draw a line of best fit on a subset of the full data set?
I want a line of best fit but only using the data in blue below
So ignore the green, yellow, orange, red, only use the blue part
Now that is easily filtered down to, so know which parts are the blue part is easy, but getting to somehow draw a line of best fit to only use the blue is the challeging part

I can filter the data set to only show the data i want to use for the line of best fit, as shown in graph below

Now all i need it to find a way to draw a line of best fit on this data set, but when i try polyfit and other best fit functions, it gets all messed up
I'm assuming because of the gaps in the dataset.
So how can i draw a line of best fit for the chart above?
5 Comments
Mathieu NOE
on 4 Jun 2021
hello
so the gaps correspond of some data exceeding a given threshold ... did you simply remove the data or you replaced it with NaN's ?
polyfit cannot work with data containing NaN's - they must be removed , this is one trick :
If the NaNs occur in the same locations in both the X and Y matrices, you can use a function call like the following, your_function( X(~isnan(X)), Y(~isnan(X)) ). If the NaNs don't occur in the same locations, you will have to first find the valid indices by something like, `X(~isnan(X)| isnan(Y))'
Scott MacKenzie
on 4 Jun 2021
Please provide the data
Rizwan Khan
on 4 Jun 2021
Rizwan Khan
on 4 Jun 2021
Scott MacKenzie
on 4 Jun 2021
You post data via the Attachments button when you are writing your question:

If you insert code in your question, it's always best to have it properly appear as code. To do this, select the code and click the code button to get the code propertly formatted in your question.

Accepted Answer
More Answers (0)
Categories
Find more on Least Squares in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


