Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Redefine 0/0

Subject: Redefine 0/0

From: Nils Tobias

Date: 30 Jul, 2010 09:30:18

Message: 1 of 6

Hi Matlab community,

Matlab normaly treats division by zero like this:
1) 0/0 = NaN
2) 1/0 = Inf

Is there a way to redefine these definitions. My problem is that I want - only in a special case (some sort of indicator function) - that 0/0 = 0. I know it's incorrect but maybe there's some way of doing it.

Thanks a lot
-NT

Subject: Redefine 0/0

From: Stefan

Date: 30 Jul, 2010 10:43:03

Message: 2 of 6


You should be careful with such interpretations. But:

a=0;
b=0;

c=a/b;

if(isnan(c) && (a==0) && (b==0))
c=0;
end

Something like this perhaps.

Regards,
Stefan


"Nils Tobias " <nils.kraemer@uni-ulm.de> wrote in message <i2u63a$p3u$1@fred.mathworks.com>...
> Hi Matlab community,
>
> Matlab normaly treats division by zero like this:
> 1) 0/0 = NaN
> 2) 1/0 = Inf
>
> Is there a way to redefine these definitions. My problem is that I want - only in a special case (some sort of indicator function) - that 0/0 = 0. I know it's incorrect but maybe there's some way of doing it.
>
> Thanks a lot
> -NT

Subject: Redefine 0/0

From: Nils Tobias

Date: 30 Jul, 2010 11:00:06

Message: 3 of 6

Thanks Stefan,

I thought there might be way to redefine this property. Anyway in my case it somehow makes sense because I could define my equation also without the division... which I probably should do.

But for now I just calculate it like you suggested with a simple M-file called wrongdivision.m

Thanks!

"Stefan" <nospam@yahoo.com> wrote in message <i2uabn$mfp$1@fred.mathworks.com>...
>
> You should be careful with such interpretations. But:
>
> a=0;
> b=0;
>
> c=a/b;
>
> if(isnan(c) && (a==0) && (b==0))
> c=0;
> end
>
> Something like this perhaps.
>
> Regards,
> Stefan
>
>
> "Nils Tobias " <nils.kraemer@uni-ulm.de> wrote in message <i2u63a$p3u$1@fred.mathworks.com>...
> > Hi Matlab community,
> >
> > Matlab normaly treats division by zero like this:
> > 1) 0/0 = NaN
> > 2) 1/0 = Inf
> >
> > Is there a way to redefine these definitions. My problem is that I want - only in a special case (some sort of indicator function) - that 0/0 = 0. I know it's incorrect but maybe there's some way of doing it.
> >
> > Thanks a lot
> > -NT

Subject: Redefine 0/0

From: Peter Perkins

Date: 30 Jul, 2010 12:44:47

Message: 4 of 6

On 7/30/2010 5:30 AM, Nils Tobias wrote:
> Is there a way to redefine these definitions. My problem is that I want
> - only in a special case (some sort of indicator function) - that 0/0 =
> 0. I know it's incorrect but maybe there's some way of doing it.

It isn't incorrect if you know that in whatever limit you're talking
about, the expression that leads to NaN has the limit zero. If you have
this

    x = 0;
    y = x.*log(x);

and x is always a non-negative real (a probability, for example), then
certainly you'd want to be able to get 0 for y. One way to do that is
to define a function

    function y = xlogx(x)
    % XLOGX x*log(x) for non-negative x
    y = x.*log(x);
    y(x==0) = 0;

and then use that in place of x.*log(x). It assumes that the input is
real and non-negative (and should probably error out if not).

The NaN you get from 0/0 just indicates that there is no way for the
division to know what the correct limit is. If you know it, then you
can impose it.

Hope this helps.

Subject: Redefine 0/0

From: Matt J

Date: 30 Jul, 2010 14:39:04

Message: 5 of 6

"Nils Tobias " <nils.kraemer@uni-ulm.de> wrote in message <i2ubbm$olu$1@fred.mathworks.com>...
> Thanks Stefan,
>
> I thought there might be way to redefine this property. Anyway in my case it somehow makes sense because I could define my equation also without the division... which I probably should do.
>
> But for now I just calculate it like you suggested with a simple M-file called wrongdivision.m
=============

You can create a specialized array type with the behavior you want using the function down at the bottom of this post, which uses this FEX tool

http://www.mathworks.com/matlabcentral/fileexchange/26611-on-the-fly-definition-of-custom-matrix-objects

It will let you do things like this

>> O=SpecialZeros(0);

>> 1/O

ans =

   Inf

>> 0/O

ans =

     0

>> O/1

ans =

     0



%%%%%%%%%%%%
function obj=SpecialZeros(A)

obj=MatrixObj;
obj.Params=A;

obj.Ops.rdivide=@wrongdivision;
obj.Ops.mrdivide=@wrongdivision;


 function out=wrongdivision(A,B)

    if isa(A,'MatrixObj'), A=A.Params; end
    if isa(B,'MatrixObj'), B=B.Params; end
      
    C=A./B;
    C( isnan(C)& A==0 )=0;
    
    %out=SpecialZeros(C);
     out=C;
     
    
 end

end

Subject: Redefine 0/0

From: Matt J

Date: 30 Jul, 2010 15:02:04

Message: 6 of 6

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i2uo68$2q8$1@fred.mathworks.com>...

> You can create a specialized array type with the behavior you want using the function down at the bottom of this post, which uses this FEX tool
>
> http://www.mathworks.com/matlabcentral/fileexchange/26611-on-the-fly-definition-of-custom-matrix-objects
=========

Truthfully though, it would probably be better to build this by creating a subclass of the MATLAB double type, as down at the bottom. Then you could more easily inherit the other mathmetical operations +,-, etc...

>> O=SpecialZeros(0);
>> [O/O, 0/O, O+1,O*3, 2-O, 1/O]

ans =

     0 0 1 0 2 Inf



%%%%%%%%%%%%%%%%%
 classdef SpecialZeros<double

 methods
     
    function obj=SpecialZeros(data)
        obj=obj@double(data);
    end

   function out=mrdivide(A,B)
      
    C=A./B;
    C( isnan(C)& A==0 )=0;
    out=C;

   end

   function out=rdivide(A,B)
      
    C=double(A)./double(B);
    C( isnan(C)& A==0 )=0;
    out=C;

   end
   
 end

end

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us