How to perform linear regression between respective columns of two matrix/table?
4 次查看(过去 30 天)
显示 更早的评论
I have two matix/ table. For example,
Table 1:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1269690/image.png)
Table 2:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1269695/image.png)
I want to fit linear regression between respective columns of the two tables. For example, column 1 in Table 1 are y values. Column 1 in Table 2 are x values. I want to get slope and intercept of linear regression between these two columns. Then the same for column 2, 3 and so on. At the end I should have n different slope and intercept pair (suppose each table has total n columns) collected in a matrix or table.
P.S.:
Edit: I would also really like to know any approach without using a for loop.
0 个评论
采纳的回答
Star Strider
2023-1-20
One approach —
A1 = randn(5,10) % Y-Values
A2 = randn(5,10) % X-Values
Rows = size(A1,1);
for k = 1:size(A1,2)
B(:,k) = [A2(:,k) ones(Rows,1)] \ A1(:,k);
end
Results = array2table(B.', 'VariableNames',{'Slope','Intercept'}, 'RowNames',compose('Col %4d',1:size(A1,2)))
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!