Linear fit over multiple Rows without using Loops (or polyfit)?
5 次查看(过去 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?
0 个评论
采纳的回答
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]
[rows, columns] = size(I)
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:
% Compute means
meanSlope = mean(coefficients(:, 1))
meanOffset = mean(coefficients(:, 2))
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!