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 of multiple variables

Subject: Function of multiple variables

From: Bo

Date: 9 Jun, 2009 10:45:03

Message: 1 of 6

Hi all
This is my first post here.
I’m quite new to MATLAB and have been using it for only a few weeks now.
My question is if there is a way to use a vector as an input for a function of n variables, such that MATLAB will evaluate the i’th element of the vector as the i’th variable input of the function.
I have found a way to do it, but it doesn’t seem very smart and it is very slow
f = strcat(f,'(',num2str(E(1), pre));
    for j = 2:n
       f = strcat(f_eva,',',num2str(E(j), pre));
    end
f = strcat(f,')');

Where f is the function, E is the vector and pre is some number.

Any help is greatly appreciated

Best regards

Bo

Subject: Function of multiple variables

From: Miroslav Balda

Date: 9 Jun, 2009 21:32:02

Message: 2 of 6

"Bo " <bfm@CeQur.net> wrote in message <h0leff$57g$1@fred.mathworks.com>...
> Hi all
> This is my first post here.
> I’m quite new to MATLAB and have been using it for only a few weeks now.
> My question is if there is a way to use a vector as an input for a function of n variables, such that MATLAB will evaluate the i’th element of the vector as the i’th variable input of the function.
> I have found a way to do it, but it doesn’t seem very smart and it is very slow
> f = strcat(f,'(',num2str(E(1), pre));
> for j = 2:n
> f = strcat(f_eva,',',num2str(E(j), pre));
> end
> f = strcat(f,')');
>
> Where f is the function, E is the vector and pre is some number.
>
> Any help is greatly appreciated
>
> Best regards
>
> Bo

Hi Bo,

I do not understand why you are trying to generate a string f(num1,num2,...,numn). It looks as if you tried to call a function f by feval function.
Why not to use directly f(E) instead, and work with E(j) in the function f. If you insist on n input parameters in the function f, you can do it in two steps, say with the following pseudocode:

[e1,e2,..,en] = arg2vars(E); % from FEX Id. 23125
[....] = f(e1,e2,..,en);

Hope it helps.
Mira

Subject: Function of multiple variables

From: John D'Errico

Date: 9 Jun, 2009 22:02:02

Message: 3 of 6

"Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <h0mkci$553$1@fred.mathworks.com>...
> "Bo " <bfm@CeQur.net> wrote in message <h0leff$57g$1@fred.mathworks.com>...
> > Hi all
> > This is my first post here.
> > I’m quite new to MATLAB and have been using it for only a few weeks now.
> > My question is if there is a way to use a vector as an input for a function of n variables, such that MATLAB will evaluate the i’th element of the vector as the i’th variable input of the function.
> > I have found a way to do it, but it doesn’t seem very smart and it is very slow
> > f = strcat(f,'(',num2str(E(1), pre));
> > for j = 2:n
> > f = strcat(f_eva,',',num2str(E(j), pre));
> > end
> > f = strcat(f,')');
> >
> > Where f is the function, E is the vector and pre is some number.
> >
> > Any help is greatly appreciated
> >
> > Best regards
> >
> > Bo
>
> Hi Bo,
>
> I do not understand why you are trying to generate a string f(num1,num2,...,numn). It looks as if you tried to call a function f by feval function.
> Why not to use directly f(E) instead, and work with E(j) in the function f. If you insist on n input parameters in the function f, you can do it in two steps, say with the following pseudocode:
>
> [e1,e2,..,en] = arg2vars(E); % from FEX Id. 23125
> [....] = f(e1,e2,..,en);
>
> Hope it helps.
> Mira

The disadvantage of this is you must explicitly name
and list each variable. If you change things so that
there is now an extra variable, then you must edit this
code. Consider an alternative which works for any
length vector E.

e = mat2cell(E(:),ones(1,numel(E)),1);
result = f(e{:});

John

Subject: Function of multiple variables

From: Bo

Date: 10 Jun, 2009 07:17:17

Message: 4 of 6

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <h0mm4q$n0$1@fred.mathworks.com>...
> "Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <h0mkci$553$1@fred.mathworks.com>...
> > "Bo " <bfm@CeQur.net> wrote in message <h0leff$57g$1@fred.mathworks.com>...
> > > Hi all
> > > This is my first post here.
> > > I’m quite new to MATLAB and have been using it for only a few weeks now.
> > > My question is if there is a way to use a vector as an input for a function of n variables, such that MATLAB will evaluate the i’th element of the vector as the i’th variable input of the function.
> > > I have found a way to do it, but it doesn’t seem very smart and it is very slow
> > > f = strcat(f,'(',num2str(E(1), pre));
> > > for j = 2:n
> > > f = strcat(f_eva,',',num2str(E(j), pre));
> > > end
> > > f = strcat(f,')');
> > >
> > > Where f is the function, E is the vector and pre is some number.
> > >
> > > Any help is greatly appreciated
> > >
> > > Best regards
> > >
> > > Bo
> >
> > Hi Bo,
> >
> > I do not understand why you are trying to generate a string f(num1,num2,...,numn). It looks as if you tried to call a function f by feval function.
> > Why not to use directly f(E) instead, and work with E(j) in the function f. If you insist on n input parameters in the function f, you can do it in two steps, say with the following pseudocode:
> >
> > [e1,e2,..,en] = arg2vars(E); % from FEX Id. 23125
> > [....] = f(e1,e2,..,en);
> >
> > Hope it helps.
> > Mira
>
> The disadvantage of this is you must explicitly name
> and list each variable. If you change things so that
> there is now an extra variable, then you must edit this
> code. Consider an alternative which works for any
> length vector E.
>
> e = mat2cell(E(:),ones(1,numel(E)),1);
> result = f(e{:});
>
> John

Oh yeah, sorry about the f_eva, I was modifying existing code to simplify it and missed that, it should read f instead.
John, that is exactly what I’m looking for, But I can seem to get it to work.
I’ve made two functions
function [c] = test2(f,E)
e = mat2cell(E(:),ones(1,numel(E)),1)
c = f(e{:}

and

function z = test1(x,y)
z=x + x*y^2
end

I defined E=[1,2]
And run test2(test1,E) and MATLAB returns an error. As I said before, I’m new to MATLAB and might be making a lot of rookie mistakes

-Bo

Subject: Function of multiple variables

From: Steven Lord

Date: 10 Jun, 2009 14:22:38

Message: 5 of 6


"Bo " <bfm@CeQur.net> wrote in message
news:h0nmlt$bpq$1@fred.mathworks.com...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message
> <h0mm4q$n0$1@fred.mathworks.com>...
>> "Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message
>> <h0mkci$553$1@fred.mathworks.com>...
>> > "Bo " <bfm@CeQur.net> wrote in message
>> > <h0leff$57g$1@fred.mathworks.com>...

*snip*

> Oh yeah, sorry about the f_eva, I was modifying existing code to simplify
> it and missed that, it should read f instead.
> John, that is exactly what I’m looking for, But I can seem to get it
> to work.
> I’ve made two functions
> function [c] = test2(f,E)
> e = mat2cell(E(:),ones(1,numel(E)),1)
> c = f(e{:}
>
> and
>
> function z = test1(x,y)
> z=x + x*y^2
> end
>
> I defined E=[1,2]
> And run test2(test1,E) and MATLAB returns an error. As I said before,
> I’m new to MATLAB and might be making a lot of rookie mistakes

When you execute the command:

test2(test1, E)

this attempts to call the test1 function with 0 input arguments and use the
output argument from that call as the first input argument to test2. Now
what happens when you call test1 with 0 input arguments? Since you didn't
specify what the first input argument (x) was, when you try to use it MATLAB
complains that it is undefined.

What you want to do is to pass in a function handle to test1 as the first
input argument to test2:

test2(@test1, E)

A function handle is an object that can be called like a function, and if
you call it will invoke the function that was used to create it. So in this
case, if f is @test1, then f(1, 2) would call test1 with two inputs -- 1 and
2. Search the documentation for "function handle" for more information.

--
Steve Lord
slord@mathworks.com

Subject: Function of multiple variables

From: Bo

Date: 10 Jun, 2009 14:31:03

Message: 6 of 6

"Steven Lord" <slord@mathworks.com> wrote in message <h0ofii$ar4$1@fred.mathworks.com>...
>
> "Bo " <bfm@CeQur.net> wrote in message
> news:h0nmlt$bpq$1@fred.mathworks.com...
> > "John D'Errico" <woodchips@rochester.rr.com> wrote in message
> > <h0mm4q$n0$1@fred.mathworks.com>...
> >> "Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message
> >> <h0mkci$553$1@fred.mathworks.com>...
> >> > "Bo " <bfm@CeQur.net> wrote in message
> >> > <h0leff$57g$1@fred.mathworks.com>...
>
> *snip*
>
> > Oh yeah, sorry about the f_eva, I was modifying existing code to simplify
> > it and missed that, it should read f instead.
> > John, that is exactly what I’m looking for, But I can seem to get it
> > to work.
> > I’ve made two functions
> > function [c] = test2(f,E)
> > e = mat2cell(E(:),ones(1,numel(E)),1)
> > c = f(e{:}
> >
> > and
> >
> > function z = test1(x,y)
> > z=x + x*y^2
> > end
> >
> > I defined E=[1,2]
> > And run test2(test1,E) and MATLAB returns an error. As I said before,
> > I’m new to MATLAB and might be making a lot of rookie mistakes
>
> When you execute the command:
>
> test2(test1, E)
>
> this attempts to call the test1 function with 0 input arguments and use the
> output argument from that call as the first input argument to test2. Now
> what happens when you call test1 with 0 input arguments? Since you didn't
> specify what the first input argument (x) was, when you try to use it MATLAB
> complains that it is undefined.
>
> What you want to do is to pass in a function handle to test1 as the first
> input argument to test2:
>
> test2(@test1, E)
>
> A function handle is an object that can be called like a function, and if
> you call it will invoke the function that was used to create it. So in this
> case, if f is @test1, then f(1, 2) would call test1 with two inputs -- 1 and
> 2. Search the documentation for "function handle" for more information.
>
> --
> Steve Lord
> slord@mathworks.com
>


Thanks Steve

That was just what I needed!

-Bo

Tags for this Thread

No tags are associated with 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