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

15 次查看(过去 30 天)
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
My mistake was this
x1fit = min(x):10:max(x);
should be this
x1fit = min(x):1:max(x);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Number Theory 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by