Doing multiplication and division within a sum()?

1 次查看(过去 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!

采纳的回答

James Tursa
James Tursa 2012-2-17
Change the offending array class to double.

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by