Effacer les filtres
Effacer les filtres

Is left divison faster than polyfit(x, y, 1)?

5 vues (au cours des 30 derniers jours)
N/A
N/A le 9 Jan 2017
Réponse apportée : Jan le 9 Jan 2017
In linear regression, results from two approach are the same.
>> x = 1:10;
>> y = x * 2 + randn(1, 10);
>> b = [ones(1, 10); x]' \ y'
b =
-0.7021
2.2412
>> p = polyfit(x, y, 1);
p =
2.2412 -0.721
I can see the implementation of `polyfit` by typing `edit polyfit`. However, I can't see the built-in function `ldivide`

Réponse acceptée

Jan
Jan le 9 Jan 2017
There is some overhead in polyfit due to the error handling. Checking the inputs is a really good idea, but if you are absolutely sure that the inputs are correct, this can save some time:
function p = fPolyFit(x, y, n)
x = x(:);
V = ones(length(x), n + 1); % Vandermonde matrix
for j = n:-1:1
V(:, j) = V(:, j + 1) .* x;
end
[Q, R] = qr(V, 0); % Solve least squares problem
p = transpose(R \ (transpose(Q) * y(:)));
And if you have to solve this with the same x and different y, storing the Vandermonde matrix can accelerate the code again.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 9 Jan 2017
If I recall correctly, polyfit creates a vandermode matrix and uses \ with it, so polyfit cannot be faster than \

Catégories

En savoir plus sur NaNs dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by