How can i convert this linear equation into a form which i can use linsolve() function?

1 回表示 (過去 30 日間)
Saadet Sena Egeli
Saadet Sena Egeli 2020 年 12 月 26 日
回答済み: Pratyush Roy 2020 年 12 月 29 日
I wrote a code like;
function [a,b]=lin_reg(x,y)
a=0;
b=0;
for i = 1:length(x)
for j = 1:length(y)
a=(y(j)-b)/x(i);
b=y(j)-a*x(i);
end
end
end
But these code didn't help me to pass my tests, so i need an equation which i can use linsolve() function.
Thanks in advance.

回答 (1 件)

Pratyush Roy
Pratyush Roy 2020 年 12 月 29 日
Hi,
Assuming that both x and y are arrays of shape 1*n, the following code snippet might be helpful
x_new = [x' ones(n,1)];
coeff = linsolve(x_new,y');
Here coeff is a 1*2 array where
a = coeff(1);
b = coeff(2);
Hope this helps!

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by