78 views (last 30 days)

It is often a mathematical convention to define 0 * Inf = 0. (For example, Shannon Entropy and the entire field of Information Theory).

However, in Matlab: 0 * Inf = NaN

Is there any way to adjust Matlab multiplication so that 0 * Inf = 0? Is there any way to localize such a modification to just a function scope?

(Note: it is easy to do this manually, but it would be simpler and more reliable to have Matlab do that automatically).

the cyclist
on 11 Sep 2014

Edited: the cyclist
on 11 Sep 2014

"*" is a syntax shorthand for the times() function, and ".*" is syntax shorthand for the mtimes() function. One way to achieve what you want is to write your own multiplication functions that first check the input arguments for this special case, and otherwise call the MATLAB ones.

MATLAB functions can be overloaded for particular classes, but I am not sure that helps you.

Matt J
on 11 Sep 2014

Strangely, in R2013b, this no longer works if times() is redefined locally. I know it worked in the past...

function test

q=inf.*0 %gives NaN

function c=times(a,b)

b(isinf(b))=0;

a(isinf(a))=0;

c=builtin('times',a,b);

end

end

Daniel Shub
on 14 Sep 2014

@matt you need to stick times in an @double folder to overload it for class double.

Sign in to comment.

Joseph Cheng
on 11 Sep 2014

Sign in to comment.

Matt J
on 11 Sep 2014

Edited: Matt J
on 11 Sep 2014

You can create a subclass of MATLAB's double data type using the attached classdef file. It will allow you to do things like this,

>> a=myclass(0); b=myclass(1); c=myclass(inf);

>> s=a+b

s =

1

>> m=a.*c

m =

0

>> entropy=-a.*log(a)

entropy =

0

You have to be a bit careful, though, because any method that you don't overload will often produce the original, non-customized double type,

>> whos s m entropy

Name Size Bytes Class Attributes

entropy 1x1 112 myclass

m 1x1 112 myclass

s 1x1 8 double

So, for example, you need to be sure that you won't be taking entropy of a sum. Or, you could of course overload summation operations to output myclass objects.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/154432-define-multiplication-by-zero-for-inf#comment_236660

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/154432-define-multiplication-by-zero-for-inf#comment_236660

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/154432-define-multiplication-by-zero-for-inf#comment_236664

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/154432-define-multiplication-by-zero-for-inf#comment_236664

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/154432-define-multiplication-by-zero-for-inf#comment_237000

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/154432-define-multiplication-by-zero-for-inf#comment_237000

Sign in to comment.