Clear Filters
Clear Filters

Issues when multipling and summing very small values in a matrix

16 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.
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
= 3.1416e-5
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;
David on 4 Mar 2015
Hi, If I am correct in thinking that bsxfun(...) will times the matrix by a value, how do I correctly times it by the constant? (yes the constant changes on the loop iteration, sorry)
Roger Stafford
Roger Stafford on 5 Mar 2015
Edited: 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:
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!