필터 지우기
필터 지우기

Fitting every fifth datapoint one after another

조회 수: 1 (최근 30일)
J.P.
J.P. 2016년 11월 8일
댓글: J.P. 2016년 11월 8일
Hello!
I want to do a linear regression for a bunch of data, using every fifth or third (maybe tenth) Datapoint. The data will be vary in size and therefor I need to do a function that varies as well.
I did the poly1-fit to the first 13 data already by typing it for each fit. Can you think of any way to solve it via function maybe a loop to solve this seccessively for any size of x- or y-datapoints.
I will be happy for any kind of response that helps me to solve this problem.
Thanks a lot!
The result should look like this for more datapoints:

채택된 답변

dbmn
dbmn 2016년 11월 8일
I have a solution that avoids the loops altogehter, but might not be as readable
n = 5; % Number of datapoints
x1 = reshape(x, n, []); % please make sure, that your number will be divisable into n Elements
y1 = reshape(y, n, []);
a = (x1-x1(1,:))\(y1-y1(1,:)); % we use mldivide here and correct for the origin
a = a(1,:);
b = y1(1,:) - a.*x1(1,:); % here we shifted back
b is the offset and a the slope of your curves
  댓글 수: 3
J.P.
J.P. 2016년 11월 8일
Hi! Thanks for your help. But your solution gives this Error:
Error using - Matrix dimensions must agree.
Error in polyfit_Inet (line 6) a = (x1-x1(1,:))\(y1-y1(1,:)); % we use mldivide here and correct for the origin
J.P.
J.P. 2016년 11월 8일
Okay, I changed the parts a little bit and it works!!!
n = 6; % Number of datapoints
x1 = reshape(x, n, []); % please make sure, that your number will be divisable into n Elements
y1 = reshape(y, n, []);
a = (x1(1)-x1(1,n))\(y1(1)-y1(1,n)); % we use mldivide here and correct for the origin
a = a(1,:);
b = y1(1,:) - a.*x1(1,:); % here we shifted back
figure()
hold on
hdl1 = plot(x1, y1, 'b.');
hdl2 = plot(x1, a.*x1(2,:)+b, 'r-');
But what I get now is a fit including 6 regression lines, which are shifted in parallel.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by