Issues when multipling and summing very small values in a matrix
22 views (last 30 days)
So I am performing a fairly simple task, where it basically multiplies all values in a matrix by a value, then summing the matrix. The issue I am having is that when these values of the matrix becomes quite low, it just sums /rounds to 0 or something.
for count1 = 1:len1
constant = 2*pi*count1*0.0100;
datamatrix = massplume_area(:, count1).*constant;
totalmassplume = sum(datamatrix(:));
It seems to be if the values are very small, or if there a quite a few zero values in the matrix 'massplume_area', then the act of multiplying it by the value 'constant' causes the output to be 0. If there are plenty of large values/ not many 0 values in massplume_area, then there is no issue.
A Jenkins on 4 Mar 2015
Your code as written only sums the value of the last column of massplume_area.*constant.
Did you mean
datamatrix(:, count1) = massplume_area(:, count1).*constant;
Roger Stafford on 4 Mar 2015
Edited: Roger Stafford on 4 Mar 2015
I think it is likely that you are not displaying your results with sufficient accuracy to see the actual values of small quantities. The smallest value in matlab's double precision format which retains its full 53-bit precision is realmin (= 2.2251e-308,) and if the results of your multiplication are no smaller than that tiny amount, they should be precise to approximately 16 decimal places. If you display a small number by itself, even the default 'format short' will show a nonzero value:
However, if you display it along with a larger number, it may look like a zero:
= 3.1416 0.0000
To display your small values accurately you should either show them alone or else use higher accuracy displays, as with 'format long', or even better, using high precision methods with 'fprintf'. For example,
x = [pi;pi*realmin];
Incidentally, you can multiply a matrix by a constant scalar without doing it column-by-column:
datamatrisx = constant*massplume_area;