Got Questions? Get Answers.
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:
Function Handles

Subject: Function Handles

From: omegayen

Date: 4 Oct, 2012 17:13:10

Message: 1 of 7

Hi,

So I was wondering if anyone knows if there is an easier to way use function handles then I am using below

My current code looks like this

t=[10^-1,10^-2,10^-3,-1+10^-4];
func = {@(x)x>t(1),@(x)x>t(2),@(x)x>t(3),@(x)x>t(4)};

If you look at the cell array func you see that every element uses

an anonymous function @(x)x>

Is there some way to rewrite func

So I don't have to rewrite @(x)x> for each element? thanks

Subject: Function Handles

From: Matt J

Date: 4 Oct, 2012 17:44:08

Message: 2 of 7

"omegayen " <omegayen@ameritech.net> wrote in message <k4kg36$hej$1@newscl01ah.mathworks.com>...
>
> Is there some way to rewrite func
>
> So I don't have to rewrite @(x)x> for each element? thanks

There might be, but you haven't told us how the func{i} are being used.

Subject: Function Handles

From: omegayen

Date: 4 Oct, 2012 18:02:08

Message: 3 of 7

"Matt J" wrote in message <k4kht8$ore$1@newscl01ah.mathworks.com>...
> "omegayen " <omegayen@ameritech.net> wrote in message <k4kg36$hej$1@newscl01ah.mathworks.com>...
> >
> > Is there some way to rewrite func
> >
> > So I don't have to rewrite @(x)x> for each element? thanks
>
> There might be, but you haven't told us how the func{i} are being used.

func{i}

is being used in such a way that I believe the way func is defined as above needs to be the same as in

func =

    @(x)x>t(1) @(x)x>t(2) @(x)x>t(3) @(x)x>t(4)

I am passing func{i} into another MATLAB function i have written

Subject: Function Handles

From: Matt J

Date: 4 Oct, 2012 18:10:08

Message: 4 of 7

"omegayen " <omegayen@ameritech.net> wrote in message <k4kiv0$t2h$1@newscl01ah.mathworks.com>...
>
> func{i}
>
> is being used in such a way that I believe the way func is defined as above needs to be the same as in
>
> func =
>
> @(x)x>t(1) @(x)x>t(2) @(x)x>t(3) @(x)x>t(4)
>
> I am passing func{i} into another MATLAB function i have written
================

Show us the code where func is used, so that we can understand what you mean when you say you want an equivalent alternative.

Subject: Function Handles

From: Steven_Lord

Date: 4 Oct, 2012 19:05:21

Message: 5 of 7



"omegayen " <omegayen@ameritech.net> wrote in message
news:k4kg36$hej$1@newscl01ah.mathworks.com...
> Hi,
>
> So I was wondering if anyone knows if there is an easier to way use
> function handles then I am using below
>
> My current code looks like this
> t=[10^-1,10^-2,10^-3,-1+10^-4];
> func = {@(x)x>t(1),@(x)x>t(2),@(x)x>t(3),@(x)x>t(4)};
>
> If you look at the cell array func you see that every element uses
>
> an anonymous function @(x)x>
>
> Is there some way to rewrite func
> So I don't have to rewrite @(x)x> for each element? thanks

func = @(x, whichElement) (x > t(whichElement));

Wherever you would have called func{n}(x) instead use func(x, n).
Wherever you would have passed func{n} into a function, instead pass @(x)
func(x, n).

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Function Handles

From: Matt J

Date: 4 Oct, 2012 19:31:08

Message: 6 of 7

"omegayen " <omegayen@ameritech.net> wrote in message <k4kg36$hej$1@newscl01ah.mathworks.com>...
>
> Is there some way to rewrite func
>
> So I don't have to rewrite @(x)x> for each element? thanks
==============

Is it possible you're trying to do a vector operation like

  func=@(x) x(:)>t(:) %x and t are vectors

Or,

  func=@(x) bsxfun(@ge,x(:),t(:).');

 

Subject: Function Handles

From: omegayen

Date: 4 Oct, 2012 23:53:08

Message: 7 of 7

"Steven_Lord" <slord@mathworks.com> wrote in message <k4kmlh$e28$1@newscl01ah.mathworks.com>...
>
>
> "omegayen " <omegayen@ameritech.net> wrote in message
> news:k4kg36$hej$1@newscl01ah.mathworks.com...
> > Hi,
> >
> > So I was wondering if anyone knows if there is an easier to way use
> > function handles then I am using below
> >
> > My current code looks like this
> > t=[10^-1,10^-2,10^-3,-1+10^-4];
> > func = {@(x)x>t(1),@(x)x>t(2),@(x)x>t(3),@(x)x>t(4)};
> >
> > If you look at the cell array func you see that every element uses
> >
> > an anonymous function @(x)x>
> >
> > Is there some way to rewrite func
> > So I don't have to rewrite @(x)x> for each element? thanks
>
> func = @(x, whichElement) (x > t(whichElement));
>
> Wherever you would have called func{n}(x) instead use func(x, n).
> Wherever you would have passed func{n} into a function, instead pass @(x)
> func(x, n).
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

This is what I was looking for, much appreciated Steve.

Thanks Matt as well.

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