필터 지우기
필터 지우기

Doing multiplication and division within a sum()?

조회 수: 1 (최근 30일)
Rachel
Rachel 2012년 2월 17일
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