Can we do polyfit on matrix?

Hi,
I have two matrix, A and B, each of 1000 rows and 100 columns. I need to do 100 polyfit on the columns. I can loop through the columns. But I am just wondering if there is any simple way to plug in A, B without the loop and return the result in an other matrix C.
Thanks,
Jennifer

回答 (3 件)

Jan
Jan 2015 年 10 月 27 日
編集済み: Jan 2015 年 10 月 27 日

0 投票

As far as I understand all columns are processed by polyfit independently. So you can at least omit the expensive checking of the inputs:
A = rand(1000, 100);
B = rand(1000, 100);
n = 3;
V = ones(1000, n + 1);
for k = 1:100
x = A(:, k);
y = B(:, k);
% Vandermonde matrix:
V(:, n+1) = 1;
for j = n:-1:1
V(:, j) = V(:, j + 1) .* x;
end
% Solve least squares problem:
[Q, R] = qr(V, 0);
p = transpose(R \ (transpose(Q) * y(:)));
...
end
I fyou need further outputs of polyfit and e.g. a normalization of the input values, explain this explicitly here. Posting your existing code is always a good idea to reduce the need to guess, what you exactly need.

3 件のコメント

JFz
JFz 2015 年 10 月 27 日
Thank you!
let me try this with my numbers.....
donald adams
donald adams 2017 年 11 月 21 日
Great solution! Thanks
Sarah
Sarah 2018 年 12 月 5 日
what if these two matrices were not of the same size? ohw would the solution change then?

サインインしてコメントする。

Jos (10584)
Jos (10584) 2015 年 10 月 27 日
編集済み: Jos (10584) 2015 年 10 月 27 日

0 投票

A loop is the most obvious choice. You can hide the loop using arrayfun
FitFH = @(k) polyfit(X(:,k), Y(:,k), 1)
P = arrayfun(FitFH, 1:size(X,2), 'un',0)
P{X} will hold the fit for the X-th columns.

4 件のコメント

JFz
JFz 2015 年 10 月 27 日
This is cool. Thank you! Let me try it too.
JFz
JFz 2015 年 10 月 27 日
What do the 'un' and 0) do in the 2nd line?
Jos (10584)
Jos (10584) 2015 年 10 月 28 日
help arrayfun
will give you the answer. The output is non-uniform.
Jos (10584)
Jos (10584) 2015 年 10 月 28 日
And by the way, you can write your own (anonymous) polyfit function that skips the input checks as Jan suggested, but this might be over your head right now.

サインインしてコメントする。

Namrata Badiger
Namrata Badiger 2020 年 5 月 28 日

0 投票

A = rand(1000, 100);
B = rand(1000, 100);
n = 3;
V = ones(1000, n + 1);
for k = 1:100
x = A(:, k);
y = B(:, k);
% Vandermonde matrix:
V(:, n+1) = 1;
for j = n:-1:1
V(:, j) = V(:, j + 1) .* x;
end
% Solve least squares problem:
[Q, R] = qr(V, 0);
p = transpose(R \ (transpose(Q) * y(:)));
...
endB = rand(1000, 100);n = 3;V = ones(1000, n + 1);for k = 1:100 x = A(:, k); y = B(:, k); % Vandermonde matrix: V(:, n+1) = 1; for j = n:-1:1 V(:, j) = V(:, j + 1) .* x; end % Solve least squares problem: [Q, R] = qr(V, 0); p = transpose(R \ (transpose(Q) * y(:))); ... end

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

タグ

質問済み:

JFz
2015 年 10 月 27 日

回答済み:

2020 年 5 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by