Linear fit over multiple Rows without using Loops (or polyfit)?

15 ビュー (過去 30 日間)
Jason
Jason 2022 年 3 月 28 日
コメント済み: Jason 2022 年 3 月 28 日
Hello, I have a matrix of data
I=[1.8,3,3.6,4.2,4.7,5.3,5.5;3.3,4.2,4.8,5.3,5.8,6.3,6.6;4.5,5.6,6.3,6.8,7.3,7.9,8.2;6.1,6.9,7.5,8,8.6,9,9.4]
I =
1.8000 3.0000 3.6000 4.2000 4.7000 5.3000 5.5000
3.3000 4.2000 4.8000 5.3000 5.8000 6.3000 6.6000
4.5000 5.6000 6.3000 6.8000 7.3000 7.9000 8.2000
6.1000 6.9000 7.5000 8.0000 8.6000 9.0000 9.4000
I would like to get the gradient of a straight line fit through each row.
i.e. I was just going to loop over all the rows something like this:
x = 1:7
1 2 3 4 5 6 7
y=I(1,:)
p=polyfit(x,y,1)
I then just want to average all those gradients (m's)
I was wondering if there was a better way to do this rather than use polyfit and loops?

採用された回答

Image Analyst
Image Analyst 2022 年 3 月 28 日
No, I don't think so. That way is fine.
I = [1.8,3,3.6,4.2,4.7,5.3,5.5;3.3,4.2,4.8,5.3,5.8,6.3,6.6;4.5,5.6,6.3,6.8,7.3,7.9,8.2;6.1,6.9,7.5,8,8.6,9,9.4]
I = 4×7
1.8000 3.0000 3.6000 4.2000 4.7000 5.3000 5.5000 3.3000 4.2000 4.8000 5.3000 5.8000 6.3000 6.6000 4.5000 5.6000 6.3000 6.8000 7.3000 7.9000 8.2000 6.1000 6.9000 7.5000 8.0000 8.6000 9.0000 9.4000
[rows, columns] = size(I)
rows = 4
columns = 7
x = 1 : columns;
coefficients = zeros(rows, 2);
for row = 1 : rows
coefficients(row, :) = polyfit(x, I(row, :), 1);
end
coefficients % Let's see them in the command window:
coefficients = 4×2
0.6000 1.6143 0.5393 3.0286 0.5964 4.2714 0.5429 5.7571
% Compute means
meanSlope = mean(coefficients(:, 1))
meanSlope = 0.5696
meanOffset = mean(coefficients(:, 2))
meanOffset = 3.6679
  4 件のコメント
Jason
Jason 2022 年 3 月 28 日
Yes, sorry, this is also more of a visualisation part.
Jason
Jason 2022 年 3 月 28 日
My mistake was this
x1fit = min(x):10:max(x);
should be this
x1fit = min(x):1:max(x);

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumber Theory についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by