Borrar filtros
Borrar filtros

Doing multiplication and division within a sum()?

1 visualización (últimos 30 días)
Rachel
Rachel el 17 de Feb. de 2012
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!

Respuesta aceptada

James Tursa
James Tursa el 17 de Feb. de 2012
Change the offending array class to double.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by