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!
댓글 수: 0
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!