Automatic clustering of data point according to line of best fit

9 views (last 30 days)
Hi All, This is one of this thing easy for an human eyes, but no so easy to code.. I have 2 vector (same length) corresponding to 2 different sensors. i plot them using a scatter plot. See image below. Most of the time the 2 sensors don't read nothing, but sometimes the sensors pick up a signal. Depending on different factor, the sensors can react differently to one another, which means the scatter plot shows multiple lines. (2 in my example, but it can be more, or less...) Does anybody has an idea how to automatically create a function which will take the 2 sensors vector as an input and return a vector the same length which would contain a label corresponding to the line in input data point correspond to?

Answers (1)

KSSV
KSSV on 23 May 2018
Given (x,y) data, you can fit s straight line using this:
N = 100 ;
x = rand(N,1) ;
y = rand(N,1) ;
plot(x,y, '.')
% fit a line
coeffs = polyfit(x, y, 1);
% Get fitted values
fittedX = linspace(min(x), max(x), 200);
fittedY = polyval(coeffs, fittedX);
% Plot the fitted line
hold on;
plot(fittedX, fittedY, 'r-')
I have used, a random data, you can use your data.
  2 Comments
KSSV
KSSV on 23 May 2018
Medric Mainson commented: Hi KSSV, Thanks for your answer. However, i don't think you understand the question. The problem is not to fit a line but to attribute the data point to the line they belong two. Thanks anyway. Cheers
KSSV
KSSV on 23 May 2018
So, you have mixed data of sensors..and you want to separate them?

Sign in to comment.

Products


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!