Doing multiplication and division within a sum()?
5 次查看(过去 30 天)
显示 更早的评论
Hello. For class, I'm writing a code that should read in a text file, find the data and place it into arrays, then find the best-fit linear line using least squares and plot it with the data. I started off using a For loop to find the best fit line (which worked without a problem), but our professor said we would be marked down for that sort of inefficiency and should use the "sum" function. However, I can't get the sum function to work. Here's the relevant portion of my code:
FileID = fopen('CO2_Data.txt');
Array = textscan(FileID, '%d %f %f', 'CommentStyle', '#');
x = Array{1};
y = Array{2};
sigma = Array{3};
%Find each sum
S_y = sum(y/sigma.^2);
S_x = sum(x/sigma.^2);
S_yx = sum(y*x/sigma.^2);
S_xx = sum(x.^2/sigma.^2);
S = sum(1/sigma.^2);
When I use / or asterisk, as shown here, I get the errors, "MTIMES is not fully supported for integer classes." and "Linear algebra is not supported for integer data types." When I change to ./ and .asterisk, as suggested by MatLab, I get the error, "Integers can only be combined with integers of the same class, or scalar doubles." S_y and S work without a problem, but the others don't. Any ideas? Thank you!
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!