简体   繁体   中英

Matlab finding best fit line from scatter plot but exclude some data points

I use Matlab to find the best fit line from a scatter plot, but I need to delete some data points. For example I am trying to find the best fit line of

x = [10 70 15 35 55 20 45 30]; y = [40 160 400 90 500 60 110 800];

Now I need to delete all y points that value is over 300, and of course deleting corresponding x points, and then make a scatter plot and find the best fit line. So how to implement this?

Now I need to delete all y points that value is over 300, and of course deleting corresponding x points,

There is standard Matlab trick - Logical Indexing (see for example inmatrix-indexing ):

x = [10 70 15 35 55 20 45 30]; y = [40 160 400 90 500 60 110 800];
filter = (y<300);
y1 = y(filter);
x1 = x(filter);
plot(x,y,'+b',x1,y1,'or');

You can use polyfit ( Matlab Doc ) function for linear fit:

ff=polyfit(x1,y1,1);
plot(x,y,'*b',x1,y1,'or',x1,ff(1)*x1 + ff(2),'-g');
grid on;

在此处输入图片说明

The best way is to logically filter the dataset, then plot it. NOTE: Data should be in column format. If it isn't, rotate like x' .

filter = (y<300);
x = x.*filter;
x = [zeros(length(x),1),x]; % this is to get the b(0) coefficient
y = y.*filter;

b = x\y;
x = x(:,2); % cleaning up column of zeros

plot(x,y,'bo')
hold on
plot([min(x),max(x)],(b(1)+b(2))*[min(x),max(x)])
hold off
axis tight

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM