Issues when multipling and summing very small values in a matrix

50 views (last 30 days)
David
David on 4 Mar 2015
Edited: Roger Stafford on 5 Mar 2015
Hi,
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;
end
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.
Any advise?

Answers (2)

A Jenkins
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
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:
format short
pi*1e-5
= 3.1416e-5
However, if you display it along with a larger number, it may look like a zero:
[pi,pi*1e-5]
= 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];
fprintf('%18.16e\n',x)
3.1415926535897931e+00
6.9902756875809187e-308
Incidentally, you can multiply a matrix by a constant scalar without doing it column-by-column:
datamatrisx = constant*massplume_area;
  4 Comments
Roger Stafford
Roger Stafford on 5 Mar 2015
What I have written is the correct way to do the multiplication (again assuming that 'massplume_area' has 'len1' columns. The 'bsxfun' automatically expands the row vector 1:len1 in duplicated rows so as to have the same number of rows as 'massplume_area'. That means that all elements of each column of 'massplume_area' get multiplied by the same value. See the documentation for 'bsxfun' at:
http://www.mathworks.com/help/matlab/ref/bsxfun.html
However, I think your main problem is accurately displaying small numbers, and using 'bsxfun' to avoid the for-loop will not solve that problem.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by