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:
isequal function handles

Subject: isequal function handles

From: Matt J

Date: 8 Feb, 2010 17:08:04

Message: 1 of 8

The help doc on isequal() does not say how it treats function handles. Experimentation shows inconsistent results, however:

>> isequal(@sin,@sin)

ans =

     1

>> isequal(@(x)x+1,@(x)x+1)

ans =

     0

Does anyone know why the case of anonymous functions does not work? Since an error was not thrown, I assume isequal is meant to do something reasonable with function handle input.

Subject: isequal function handles

From: Oleg Komarov

Date: 8 Feb, 2010 17:23:04

Message: 2 of 8

"Matt J "
> The help doc on isequal() does not say how it treats function handles. Experimentation shows inconsistent results, however:
>
> >> isequal(@sin,@sin)
>
> ans =
>
> 1
>
> >> isequal(@(x)x+1,@(x)x+1)
>
> ans =
>
> 0
>
> Does anyone know why the case of anonymous functions does not work? Since an error was not thrown, I assume isequal is meant to do something reasonable with function handle input.

Same 'inconsistency' found on Win server2008 64bit Mat2009b

Oleg

Subject: isequal function handles

From: Peter

Date: 8 Feb, 2010 17:24:49

Message: 3 of 8

On Feb 8, 9:08 am, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> The help doc on isequal() does not say how it treats function handles. Experimentation shows inconsistent results, however:
>
> >> isequal(@sin,@sin)
>
> ans =
>
>      1
>
> >> isequal(@(x)x+1,@(x)x+1)
>
> ans =
>
>      0
>
> Does anyone know why the case of anonymous functions does not work? Since an error was not thrown, I assume isequal is meant to do something reasonable with function handle input.

Here is my rationale as to why the results you obtained are actually
consistent...

In the first test, you generated a pair of handles, both of which
point to the same built-in function, and both of which are therefore
considered equal. For your second example, Matlab has no way of
knowing that the two anonymous functions are identical. Each will be
individually encapsulated. Since the two handles do not point to the
same regions of memory, they are therefore considered not equal.

The following example:

>> h1 = @(x) x^2 + 1
h1 =
    @(x)x^2+1
>> h2 = h1
h2 =
    @(x)x^2+1
>> isequal(h1,h2)
ans =
     1

shows that h1 and h2 are considered equal if they both reference the
same anonymous function definition, as you would expect.

HTH,

--Peter

Subject: isequal function handles

From: Matt J

Date: 8 Feb, 2010 18:16:04

Message: 4 of 8

Peter <petersamsimon2@hotmail.com> wrote in message <6a8e4b21-8a4b-49f9-8e36-7bf5ecd60826@b7g2000yqd.googlegroups.com>...

> Since the two handles do not point to the
> same regions of memory, they are therefore considered not equal.
================

That doesn't quite rationalize it for me. ISEQUAL is supposed to examine the contents of the variables, not just the region of memory they occupy. In the following simple example, a and b do not point to the same memory either, yet isequal correctly judges they as equal.

>> a=1;b=1; isequal(a,b)

ans =

     1

Subject: isequal function handles

From: Oleg Komarov

Date: 8 Feb, 2010 18:31:05

Message: 5 of 8

"Matt J "
> Peter
>
> > Since the two handles do not point to the
> > same regions of memory, they are therefore considered not equal.
> ================
>
> That doesn't quite rationalize it for me. ISEQUAL is supposed to examine the contents of the variables, not just the region of memory they occupy. In the following simple example, a and b do not point to the same memory either, yet isequal correctly judges they as equal.
>
> >> a=1;b=1; isequal(a,b)
>
> ans =
>
> 1

I think clarification from TMW is needed here. I see it as an inconsistency.

Oleg

Subject: isequal function handles

From: Matt J

Date: 8 Feb, 2010 18:33:06

Message: 6 of 8

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hkpkd4$2e0$1@fred.mathworks.com>...
> Peter <petersamsimon2@hotmail.com> wrote in message <6a8e4b21-8a4b-49f9-8e36-7bf5ecd60826@b7g2000yqd.googlegroups.com>...
>
> > Since the two handles do not point to the
> > same regions of memory, they are therefore considered not equal.
> ================
>
> That doesn't quite rationalize it for me. ISEQUAL is supposed to examine the contents of the variables, not just the region of memory they occupy. In the following simple example, a and b do not point to the same memory either, yet isequal correctly judges they as equal.
>
> >> a=1;b=1; isequal(a,b)
>
> ans =
>
> 1

Or do you mean that a value variable, like a=1 and b=1, is a copy-on-write pointer to a piece of data whereas a handle variable is a copy-on-write pointer to another pointer? That would explain it, I suppose...

Subject: isequal function handles

From: Matt J

Date: 8 Feb, 2010 18:51:04

Message: 7 of 8

Just to add to the mix, the same phenomenon occurs for nested functions as for anonymous functions:


function h=test

h=@nested;

    function out=nested
        
        out=1;
        
    end

end



>> isequal(test,test)

ans =

     0

Subject: isequal function handles

From: Patrick

Date: 2 Sep, 2013 09:08:09

Message: 8 of 8

There's now documentation on the topic:
http://www.mathworks.se/help/matlab/matlab_prog/advanced-operations-on-function-handles.html

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