Error using Polyfit: The first two inputs must have the same number of elements. The x can have multiple inputs as columns?

조회 수: 47 (최근 30일)
x = t_result(:,2:8);
y = t_result(:,11);
p1 = polyfit(x,y,1);
f1 = polyval(p1, x);
p2 = polyfit(x,y,2);
f2 = polyval(p2, x);
res1 = polyval(p1,x) - y;
res2 = polyval(p2, x) - y;
subplot(2,2,1), plot(x,y,'ko'), hold on, plot(x,f1,'k--'), grid on, title('1st Order Fit')
subplot(2,2,2), plot(x,y,'ko'), hold on, plot(x,f2,'k--'), grid on, title('2nd Order Fit')

답변 (2개)

KSSV
KSSV 2020년 9월 17일
편집: KSSV 2020년 9월 17일
Your x is a m*6 array and y is a m*1..It is not allowed. That is why you got error.
Run a loop and use polyfit for each column of x.
If you are looking to fit a line with multiple inputs.....Have a look on regression.

Walter Roberson
Walter Roberson 2020년 9월 17일
The x can have multiple inputs as columns?
doc polyfit
"If x is not a vector, then polyfit converts it into a column vector x(:)."
"If y is not a vector, then polyfit converts it into a column vector y(:)."
You cannot use polyfit() to fit a multi-dimensional x against a y value.
I speculate that you might be wanting:
p1 = [x, ones(size(x,1),1)] \ y;
This should give you a size(x,2)+1 vector of results, such that
p1(1)*x(:,1) + p1(2)*x(:,2) + p1(3)*x(:,3) + p1(4)*x(:,4) + p1(5)*x(:,5) + p1(6)*x(:,6) + p1(7)*x(:,7) + p1(8)*1 best approximates y

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by