refer to second function output in MATLAB
up vote
0
down vote
favorite
For a project I have a list of possible activation functions to choose from, depending on whats chosen in the configuration.
i want these to all be g() so that g is a changeable function.
Also in each function file, i have defined the derivative as a function Dg():
function [g Dg] = identity(x)
g = x;
Dg = 1
end
I can refer to the first output of the function:
g = @identity
but how do i define Dg() in a similar way?
matlab
add a comment |
up vote
0
down vote
favorite
For a project I have a list of possible activation functions to choose from, depending on whats chosen in the configuration.
i want these to all be g() so that g is a changeable function.
Also in each function file, i have defined the derivative as a function Dg():
function [g Dg] = identity(x)
g = x;
Dg = 1
end
I can refer to the first output of the function:
g = @identity
but how do i define Dg() in a similar way?
matlab
2
The@
symbol forms a handle to either the named function that follows the@
sign, or to the anonymous function that follows the@
sign.g=@identify
does not give you the first output but gives your functionidentify
another nameg
so you can use it as[g Dg] = g(x)
. Read more here.
– Anthony
Nov 22 at 12:10
Honestly, I can read the question in 5 different ways ... and I see answers trying to answer different things. Try to explain a bit more explicitely what is it you want to achieve or to get as output, and also please do not reuse symbols names in the same code snipet (yourg
variable is used in different places with the same name while they are completely different variables with different scopes).
– Hoki
Nov 22 at 18:04
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
For a project I have a list of possible activation functions to choose from, depending on whats chosen in the configuration.
i want these to all be g() so that g is a changeable function.
Also in each function file, i have defined the derivative as a function Dg():
function [g Dg] = identity(x)
g = x;
Dg = 1
end
I can refer to the first output of the function:
g = @identity
but how do i define Dg() in a similar way?
matlab
For a project I have a list of possible activation functions to choose from, depending on whats chosen in the configuration.
i want these to all be g() so that g is a changeable function.
Also in each function file, i have defined the derivative as a function Dg():
function [g Dg] = identity(x)
g = x;
Dg = 1
end
I can refer to the first output of the function:
g = @identity
but how do i define Dg() in a similar way?
matlab
matlab
edited Nov 22 at 11:46
Ander Biguri
26k105390
26k105390
asked Nov 22 at 11:28
Mikael Wendt
41
41
2
The@
symbol forms a handle to either the named function that follows the@
sign, or to the anonymous function that follows the@
sign.g=@identify
does not give you the first output but gives your functionidentify
another nameg
so you can use it as[g Dg] = g(x)
. Read more here.
– Anthony
Nov 22 at 12:10
Honestly, I can read the question in 5 different ways ... and I see answers trying to answer different things. Try to explain a bit more explicitely what is it you want to achieve or to get as output, and also please do not reuse symbols names in the same code snipet (yourg
variable is used in different places with the same name while they are completely different variables with different scopes).
– Hoki
Nov 22 at 18:04
add a comment |
2
The@
symbol forms a handle to either the named function that follows the@
sign, or to the anonymous function that follows the@
sign.g=@identify
does not give you the first output but gives your functionidentify
another nameg
so you can use it as[g Dg] = g(x)
. Read more here.
– Anthony
Nov 22 at 12:10
Honestly, I can read the question in 5 different ways ... and I see answers trying to answer different things. Try to explain a bit more explicitely what is it you want to achieve or to get as output, and also please do not reuse symbols names in the same code snipet (yourg
variable is used in different places with the same name while they are completely different variables with different scopes).
– Hoki
Nov 22 at 18:04
2
2
The
@
symbol forms a handle to either the named function that follows the @
sign, or to the anonymous function that follows the @
sign. g=@identify
does not give you the first output but gives your function identify
another name g
so you can use it as [g Dg] = g(x)
. Read more here.– Anthony
Nov 22 at 12:10
The
@
symbol forms a handle to either the named function that follows the @
sign, or to the anonymous function that follows the @
sign. g=@identify
does not give you the first output but gives your function identify
another name g
so you can use it as [g Dg] = g(x)
. Read more here.– Anthony
Nov 22 at 12:10
Honestly, I can read the question in 5 different ways ... and I see answers trying to answer different things. Try to explain a bit more explicitely what is it you want to achieve or to get as output, and also please do not reuse symbols names in the same code snipet (your
g
variable is used in different places with the same name while they are completely different variables with different scopes).– Hoki
Nov 22 at 18:04
Honestly, I can read the question in 5 different ways ... and I see answers trying to answer different things. Try to explain a bit more explicitely what is it you want to achieve or to get as output, and also please do not reuse symbols names in the same code snipet (your
g
variable is used in different places with the same name while they are completely different variables with different scopes).– Hoki
Nov 22 at 18:04
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
You are wrong, you are not referring to the first output of the function, you are referring to the entire function like that. in your example, g
becomes identity
.
You can test this by doing:
g = @identity;
[out1,out2]=g(5)
You can rename it as much as you want
mrpotato = @identity;
[out1,out2]=mrpotato(5)
Possibly the fact that when you call a function without brackets in the output it only returns 1 output and in conjunction you named the function the same as the first output mislead you to think you are catching the first output, but this is not the case, you are just copying the entire function and calling it g
@
is an operator to define/reference functions.
It's not a solution. the question is how to "refer to second function output in MATLAB".
– OmG
Nov 22 at 11:50
@OmG what do you mean? I literally show how to do it :/. The proposition of the problem was wrong, that needed to be tackled.
– Ander Biguri
Nov 22 at 11:52
He wanna refer to the second output as a function! not to get the result of theidentity
function after applying the identity on a value. Something like@Dg
. You know.
– OmG
Nov 22 at 11:54
@OmG that would be the case, with a different wording. I am here interpreting that OP thinks that theirg
is the first output, and it plainly is not. So the premise is wrong, as they are not doing what they think they are, therefore their objective (@Dg
) comes from a derivative logic from a flawed proposition. I am pointing that out. DefiningDg()
in a similar way tog
is doingDg=g
, but I am sure that is not what OP thought.
– Ander Biguri
Nov 22 at 11:58
I see. But the OP wants to refer to the outputs as a function and your explanation does not solve his problem at all. Indeed, you are explaining his fault to refer to the first output as a function. Anyhow, it might be satisfiable for the OP.
– OmG
Nov 22 at 12:05
|
show 3 more comments
up vote
0
down vote
Calls to a function with a single output in the expression, such as a=something();
, will always return the first output of something()
, whether it is a function or a handle to a function.
A workaround would be to make a function designed to return the second (or n-th) output from whatever handle it is given and pipe @identity (or any other handle) through it :
function out=take_second_output(fun,input)
%fun : function handle
%input: value
[~,out]=fun(input);
end
% More generic version,:
% - works with any number of arguments
% - any of the outputs can be picked
%
% Calling 'a=take_nth_output(fun,n,arg1,arg2,...)' is equivalent to
% '[~,~,...(n-1) times ...,a] = fun(arg1,arg2,...)'
function out=take_nth_output(fun,n_out,varargin)
%fun : function handle
%n_out: index of the output to be returned
out_tmp=cell(n_out,1);
out_tmp{:} = fun(varargin{:});
out = out_tmp{n_out};
end
% define function handle d
d = @identity;
% define Dg as an anonymous handle
Dg = @(x) take_second_output(@identity,x);
Dg_generic = @(x) take_nth_output(@identity,2,x);
1
function handles are expected to have a single output. no it is not true. Trym=@min; [minimum,index]=m([23, 12])
. Otherwise the answer is right, just note that you can however still do[~,Dg]=g()
in this case
– Ander Biguri
Nov 22 at 12:01
You are right, this is poorly stated. What I meant is that statementa = something();
will always return the first output regardless whether something() is a function or a handle to a function, and any additional output will be discarded. I'll rephrase that.
– Brice
Nov 22 at 13:01
add a comment |
up vote
0
down vote
If you want to obtain functions g
and Dg
, you probably want to return function handles instead. Something like this:
function [g,Dg] = identity
g = @(x) x;
Dg = @(x) 1;
end
Now this line:
[g,Dg] = identity;
will give you two functions, which you can use as:
y = g(x);
dy = Dg(x);
A more complicated example, the actual functions returned here are not all that complex, but this shows the mechanics. You can create complicated functions with tunable parameters, flow control, etc.:
function [g,Dg] = complicated(scale)
g = @func;
Dg = @deriv_func;
function y = func(x)
y = cos*scale(x);
end
function y = deriv_func(x)
y = -scale*sin(scale*x);
end
end
Similarly to before, you now do:
[g,Dg] = complicated(4.7);
to get your function handles. The 4.7 will be “embedded” in those handles, meaning that it affects the meaning of the functions g
and Dg
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429975%2frefer-to-second-function-output-in-matlab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You are wrong, you are not referring to the first output of the function, you are referring to the entire function like that. in your example, g
becomes identity
.
You can test this by doing:
g = @identity;
[out1,out2]=g(5)
You can rename it as much as you want
mrpotato = @identity;
[out1,out2]=mrpotato(5)
Possibly the fact that when you call a function without brackets in the output it only returns 1 output and in conjunction you named the function the same as the first output mislead you to think you are catching the first output, but this is not the case, you are just copying the entire function and calling it g
@
is an operator to define/reference functions.
It's not a solution. the question is how to "refer to second function output in MATLAB".
– OmG
Nov 22 at 11:50
@OmG what do you mean? I literally show how to do it :/. The proposition of the problem was wrong, that needed to be tackled.
– Ander Biguri
Nov 22 at 11:52
He wanna refer to the second output as a function! not to get the result of theidentity
function after applying the identity on a value. Something like@Dg
. You know.
– OmG
Nov 22 at 11:54
@OmG that would be the case, with a different wording. I am here interpreting that OP thinks that theirg
is the first output, and it plainly is not. So the premise is wrong, as they are not doing what they think they are, therefore their objective (@Dg
) comes from a derivative logic from a flawed proposition. I am pointing that out. DefiningDg()
in a similar way tog
is doingDg=g
, but I am sure that is not what OP thought.
– Ander Biguri
Nov 22 at 11:58
I see. But the OP wants to refer to the outputs as a function and your explanation does not solve his problem at all. Indeed, you are explaining his fault to refer to the first output as a function. Anyhow, it might be satisfiable for the OP.
– OmG
Nov 22 at 12:05
|
show 3 more comments
up vote
0
down vote
You are wrong, you are not referring to the first output of the function, you are referring to the entire function like that. in your example, g
becomes identity
.
You can test this by doing:
g = @identity;
[out1,out2]=g(5)
You can rename it as much as you want
mrpotato = @identity;
[out1,out2]=mrpotato(5)
Possibly the fact that when you call a function without brackets in the output it only returns 1 output and in conjunction you named the function the same as the first output mislead you to think you are catching the first output, but this is not the case, you are just copying the entire function and calling it g
@
is an operator to define/reference functions.
It's not a solution. the question is how to "refer to second function output in MATLAB".
– OmG
Nov 22 at 11:50
@OmG what do you mean? I literally show how to do it :/. The proposition of the problem was wrong, that needed to be tackled.
– Ander Biguri
Nov 22 at 11:52
He wanna refer to the second output as a function! not to get the result of theidentity
function after applying the identity on a value. Something like@Dg
. You know.
– OmG
Nov 22 at 11:54
@OmG that would be the case, with a different wording. I am here interpreting that OP thinks that theirg
is the first output, and it plainly is not. So the premise is wrong, as they are not doing what they think they are, therefore their objective (@Dg
) comes from a derivative logic from a flawed proposition. I am pointing that out. DefiningDg()
in a similar way tog
is doingDg=g
, but I am sure that is not what OP thought.
– Ander Biguri
Nov 22 at 11:58
I see. But the OP wants to refer to the outputs as a function and your explanation does not solve his problem at all. Indeed, you are explaining his fault to refer to the first output as a function. Anyhow, it might be satisfiable for the OP.
– OmG
Nov 22 at 12:05
|
show 3 more comments
up vote
0
down vote
up vote
0
down vote
You are wrong, you are not referring to the first output of the function, you are referring to the entire function like that. in your example, g
becomes identity
.
You can test this by doing:
g = @identity;
[out1,out2]=g(5)
You can rename it as much as you want
mrpotato = @identity;
[out1,out2]=mrpotato(5)
Possibly the fact that when you call a function without brackets in the output it only returns 1 output and in conjunction you named the function the same as the first output mislead you to think you are catching the first output, but this is not the case, you are just copying the entire function and calling it g
@
is an operator to define/reference functions.
You are wrong, you are not referring to the first output of the function, you are referring to the entire function like that. in your example, g
becomes identity
.
You can test this by doing:
g = @identity;
[out1,out2]=g(5)
You can rename it as much as you want
mrpotato = @identity;
[out1,out2]=mrpotato(5)
Possibly the fact that when you call a function without brackets in the output it only returns 1 output and in conjunction you named the function the same as the first output mislead you to think you are catching the first output, but this is not the case, you are just copying the entire function and calling it g
@
is an operator to define/reference functions.
edited Nov 22 at 11:51
answered Nov 22 at 11:48
Ander Biguri
26k105390
26k105390
It's not a solution. the question is how to "refer to second function output in MATLAB".
– OmG
Nov 22 at 11:50
@OmG what do you mean? I literally show how to do it :/. The proposition of the problem was wrong, that needed to be tackled.
– Ander Biguri
Nov 22 at 11:52
He wanna refer to the second output as a function! not to get the result of theidentity
function after applying the identity on a value. Something like@Dg
. You know.
– OmG
Nov 22 at 11:54
@OmG that would be the case, with a different wording. I am here interpreting that OP thinks that theirg
is the first output, and it plainly is not. So the premise is wrong, as they are not doing what they think they are, therefore their objective (@Dg
) comes from a derivative logic from a flawed proposition. I am pointing that out. DefiningDg()
in a similar way tog
is doingDg=g
, but I am sure that is not what OP thought.
– Ander Biguri
Nov 22 at 11:58
I see. But the OP wants to refer to the outputs as a function and your explanation does not solve his problem at all. Indeed, you are explaining his fault to refer to the first output as a function. Anyhow, it might be satisfiable for the OP.
– OmG
Nov 22 at 12:05
|
show 3 more comments
It's not a solution. the question is how to "refer to second function output in MATLAB".
– OmG
Nov 22 at 11:50
@OmG what do you mean? I literally show how to do it :/. The proposition of the problem was wrong, that needed to be tackled.
– Ander Biguri
Nov 22 at 11:52
He wanna refer to the second output as a function! not to get the result of theidentity
function after applying the identity on a value. Something like@Dg
. You know.
– OmG
Nov 22 at 11:54
@OmG that would be the case, with a different wording. I am here interpreting that OP thinks that theirg
is the first output, and it plainly is not. So the premise is wrong, as they are not doing what they think they are, therefore their objective (@Dg
) comes from a derivative logic from a flawed proposition. I am pointing that out. DefiningDg()
in a similar way tog
is doingDg=g
, but I am sure that is not what OP thought.
– Ander Biguri
Nov 22 at 11:58
I see. But the OP wants to refer to the outputs as a function and your explanation does not solve his problem at all. Indeed, you are explaining his fault to refer to the first output as a function. Anyhow, it might be satisfiable for the OP.
– OmG
Nov 22 at 12:05
It's not a solution. the question is how to "refer to second function output in MATLAB".
– OmG
Nov 22 at 11:50
It's not a solution. the question is how to "refer to second function output in MATLAB".
– OmG
Nov 22 at 11:50
@OmG what do you mean? I literally show how to do it :/. The proposition of the problem was wrong, that needed to be tackled.
– Ander Biguri
Nov 22 at 11:52
@OmG what do you mean? I literally show how to do it :/. The proposition of the problem was wrong, that needed to be tackled.
– Ander Biguri
Nov 22 at 11:52
He wanna refer to the second output as a function! not to get the result of the
identity
function after applying the identity on a value. Something like @Dg
. You know.– OmG
Nov 22 at 11:54
He wanna refer to the second output as a function! not to get the result of the
identity
function after applying the identity on a value. Something like @Dg
. You know.– OmG
Nov 22 at 11:54
@OmG that would be the case, with a different wording. I am here interpreting that OP thinks that their
g
is the first output, and it plainly is not. So the premise is wrong, as they are not doing what they think they are, therefore their objective (@Dg
) comes from a derivative logic from a flawed proposition. I am pointing that out. Defining Dg()
in a similar way to g
is doing Dg=g
, but I am sure that is not what OP thought.– Ander Biguri
Nov 22 at 11:58
@OmG that would be the case, with a different wording. I am here interpreting that OP thinks that their
g
is the first output, and it plainly is not. So the premise is wrong, as they are not doing what they think they are, therefore their objective (@Dg
) comes from a derivative logic from a flawed proposition. I am pointing that out. Defining Dg()
in a similar way to g
is doing Dg=g
, but I am sure that is not what OP thought.– Ander Biguri
Nov 22 at 11:58
I see. But the OP wants to refer to the outputs as a function and your explanation does not solve his problem at all. Indeed, you are explaining his fault to refer to the first output as a function. Anyhow, it might be satisfiable for the OP.
– OmG
Nov 22 at 12:05
I see. But the OP wants to refer to the outputs as a function and your explanation does not solve his problem at all. Indeed, you are explaining his fault to refer to the first output as a function. Anyhow, it might be satisfiable for the OP.
– OmG
Nov 22 at 12:05
|
show 3 more comments
up vote
0
down vote
Calls to a function with a single output in the expression, such as a=something();
, will always return the first output of something()
, whether it is a function or a handle to a function.
A workaround would be to make a function designed to return the second (or n-th) output from whatever handle it is given and pipe @identity (or any other handle) through it :
function out=take_second_output(fun,input)
%fun : function handle
%input: value
[~,out]=fun(input);
end
% More generic version,:
% - works with any number of arguments
% - any of the outputs can be picked
%
% Calling 'a=take_nth_output(fun,n,arg1,arg2,...)' is equivalent to
% '[~,~,...(n-1) times ...,a] = fun(arg1,arg2,...)'
function out=take_nth_output(fun,n_out,varargin)
%fun : function handle
%n_out: index of the output to be returned
out_tmp=cell(n_out,1);
out_tmp{:} = fun(varargin{:});
out = out_tmp{n_out};
end
% define function handle d
d = @identity;
% define Dg as an anonymous handle
Dg = @(x) take_second_output(@identity,x);
Dg_generic = @(x) take_nth_output(@identity,2,x);
1
function handles are expected to have a single output. no it is not true. Trym=@min; [minimum,index]=m([23, 12])
. Otherwise the answer is right, just note that you can however still do[~,Dg]=g()
in this case
– Ander Biguri
Nov 22 at 12:01
You are right, this is poorly stated. What I meant is that statementa = something();
will always return the first output regardless whether something() is a function or a handle to a function, and any additional output will be discarded. I'll rephrase that.
– Brice
Nov 22 at 13:01
add a comment |
up vote
0
down vote
Calls to a function with a single output in the expression, such as a=something();
, will always return the first output of something()
, whether it is a function or a handle to a function.
A workaround would be to make a function designed to return the second (or n-th) output from whatever handle it is given and pipe @identity (or any other handle) through it :
function out=take_second_output(fun,input)
%fun : function handle
%input: value
[~,out]=fun(input);
end
% More generic version,:
% - works with any number of arguments
% - any of the outputs can be picked
%
% Calling 'a=take_nth_output(fun,n,arg1,arg2,...)' is equivalent to
% '[~,~,...(n-1) times ...,a] = fun(arg1,arg2,...)'
function out=take_nth_output(fun,n_out,varargin)
%fun : function handle
%n_out: index of the output to be returned
out_tmp=cell(n_out,1);
out_tmp{:} = fun(varargin{:});
out = out_tmp{n_out};
end
% define function handle d
d = @identity;
% define Dg as an anonymous handle
Dg = @(x) take_second_output(@identity,x);
Dg_generic = @(x) take_nth_output(@identity,2,x);
1
function handles are expected to have a single output. no it is not true. Trym=@min; [minimum,index]=m([23, 12])
. Otherwise the answer is right, just note that you can however still do[~,Dg]=g()
in this case
– Ander Biguri
Nov 22 at 12:01
You are right, this is poorly stated. What I meant is that statementa = something();
will always return the first output regardless whether something() is a function or a handle to a function, and any additional output will be discarded. I'll rephrase that.
– Brice
Nov 22 at 13:01
add a comment |
up vote
0
down vote
up vote
0
down vote
Calls to a function with a single output in the expression, such as a=something();
, will always return the first output of something()
, whether it is a function or a handle to a function.
A workaround would be to make a function designed to return the second (or n-th) output from whatever handle it is given and pipe @identity (or any other handle) through it :
function out=take_second_output(fun,input)
%fun : function handle
%input: value
[~,out]=fun(input);
end
% More generic version,:
% - works with any number of arguments
% - any of the outputs can be picked
%
% Calling 'a=take_nth_output(fun,n,arg1,arg2,...)' is equivalent to
% '[~,~,...(n-1) times ...,a] = fun(arg1,arg2,...)'
function out=take_nth_output(fun,n_out,varargin)
%fun : function handle
%n_out: index of the output to be returned
out_tmp=cell(n_out,1);
out_tmp{:} = fun(varargin{:});
out = out_tmp{n_out};
end
% define function handle d
d = @identity;
% define Dg as an anonymous handle
Dg = @(x) take_second_output(@identity,x);
Dg_generic = @(x) take_nth_output(@identity,2,x);
Calls to a function with a single output in the expression, such as a=something();
, will always return the first output of something()
, whether it is a function or a handle to a function.
A workaround would be to make a function designed to return the second (or n-th) output from whatever handle it is given and pipe @identity (or any other handle) through it :
function out=take_second_output(fun,input)
%fun : function handle
%input: value
[~,out]=fun(input);
end
% More generic version,:
% - works with any number of arguments
% - any of the outputs can be picked
%
% Calling 'a=take_nth_output(fun,n,arg1,arg2,...)' is equivalent to
% '[~,~,...(n-1) times ...,a] = fun(arg1,arg2,...)'
function out=take_nth_output(fun,n_out,varargin)
%fun : function handle
%n_out: index of the output to be returned
out_tmp=cell(n_out,1);
out_tmp{:} = fun(varargin{:});
out = out_tmp{n_out};
end
% define function handle d
d = @identity;
% define Dg as an anonymous handle
Dg = @(x) take_second_output(@identity,x);
Dg_generic = @(x) take_nth_output(@identity,2,x);
edited Nov 22 at 13:14
answered Nov 22 at 11:53
Brice
1,300110
1,300110
1
function handles are expected to have a single output. no it is not true. Trym=@min; [minimum,index]=m([23, 12])
. Otherwise the answer is right, just note that you can however still do[~,Dg]=g()
in this case
– Ander Biguri
Nov 22 at 12:01
You are right, this is poorly stated. What I meant is that statementa = something();
will always return the first output regardless whether something() is a function or a handle to a function, and any additional output will be discarded. I'll rephrase that.
– Brice
Nov 22 at 13:01
add a comment |
1
function handles are expected to have a single output. no it is not true. Trym=@min; [minimum,index]=m([23, 12])
. Otherwise the answer is right, just note that you can however still do[~,Dg]=g()
in this case
– Ander Biguri
Nov 22 at 12:01
You are right, this is poorly stated. What I meant is that statementa = something();
will always return the first output regardless whether something() is a function or a handle to a function, and any additional output will be discarded. I'll rephrase that.
– Brice
Nov 22 at 13:01
1
1
function handles are expected to have a single output. no it is not true. Try
m=@min; [minimum,index]=m([23, 12])
. Otherwise the answer is right, just note that you can however still do [~,Dg]=g()
in this case– Ander Biguri
Nov 22 at 12:01
function handles are expected to have a single output. no it is not true. Try
m=@min; [minimum,index]=m([23, 12])
. Otherwise the answer is right, just note that you can however still do [~,Dg]=g()
in this case– Ander Biguri
Nov 22 at 12:01
You are right, this is poorly stated. What I meant is that statement
a = something();
will always return the first output regardless whether something() is a function or a handle to a function, and any additional output will be discarded. I'll rephrase that.– Brice
Nov 22 at 13:01
You are right, this is poorly stated. What I meant is that statement
a = something();
will always return the first output regardless whether something() is a function or a handle to a function, and any additional output will be discarded. I'll rephrase that.– Brice
Nov 22 at 13:01
add a comment |
up vote
0
down vote
If you want to obtain functions g
and Dg
, you probably want to return function handles instead. Something like this:
function [g,Dg] = identity
g = @(x) x;
Dg = @(x) 1;
end
Now this line:
[g,Dg] = identity;
will give you two functions, which you can use as:
y = g(x);
dy = Dg(x);
A more complicated example, the actual functions returned here are not all that complex, but this shows the mechanics. You can create complicated functions with tunable parameters, flow control, etc.:
function [g,Dg] = complicated(scale)
g = @func;
Dg = @deriv_func;
function y = func(x)
y = cos*scale(x);
end
function y = deriv_func(x)
y = -scale*sin(scale*x);
end
end
Similarly to before, you now do:
[g,Dg] = complicated(4.7);
to get your function handles. The 4.7 will be “embedded” in those handles, meaning that it affects the meaning of the functions g
and Dg
.
add a comment |
up vote
0
down vote
If you want to obtain functions g
and Dg
, you probably want to return function handles instead. Something like this:
function [g,Dg] = identity
g = @(x) x;
Dg = @(x) 1;
end
Now this line:
[g,Dg] = identity;
will give you two functions, which you can use as:
y = g(x);
dy = Dg(x);
A more complicated example, the actual functions returned here are not all that complex, but this shows the mechanics. You can create complicated functions with tunable parameters, flow control, etc.:
function [g,Dg] = complicated(scale)
g = @func;
Dg = @deriv_func;
function y = func(x)
y = cos*scale(x);
end
function y = deriv_func(x)
y = -scale*sin(scale*x);
end
end
Similarly to before, you now do:
[g,Dg] = complicated(4.7);
to get your function handles. The 4.7 will be “embedded” in those handles, meaning that it affects the meaning of the functions g
and Dg
.
add a comment |
up vote
0
down vote
up vote
0
down vote
If you want to obtain functions g
and Dg
, you probably want to return function handles instead. Something like this:
function [g,Dg] = identity
g = @(x) x;
Dg = @(x) 1;
end
Now this line:
[g,Dg] = identity;
will give you two functions, which you can use as:
y = g(x);
dy = Dg(x);
A more complicated example, the actual functions returned here are not all that complex, but this shows the mechanics. You can create complicated functions with tunable parameters, flow control, etc.:
function [g,Dg] = complicated(scale)
g = @func;
Dg = @deriv_func;
function y = func(x)
y = cos*scale(x);
end
function y = deriv_func(x)
y = -scale*sin(scale*x);
end
end
Similarly to before, you now do:
[g,Dg] = complicated(4.7);
to get your function handles. The 4.7 will be “embedded” in those handles, meaning that it affects the meaning of the functions g
and Dg
.
If you want to obtain functions g
and Dg
, you probably want to return function handles instead. Something like this:
function [g,Dg] = identity
g = @(x) x;
Dg = @(x) 1;
end
Now this line:
[g,Dg] = identity;
will give you two functions, which you can use as:
y = g(x);
dy = Dg(x);
A more complicated example, the actual functions returned here are not all that complex, but this shows the mechanics. You can create complicated functions with tunable parameters, flow control, etc.:
function [g,Dg] = complicated(scale)
g = @func;
Dg = @deriv_func;
function y = func(x)
y = cos*scale(x);
end
function y = deriv_func(x)
y = -scale*sin(scale*x);
end
end
Similarly to before, you now do:
[g,Dg] = complicated(4.7);
to get your function handles. The 4.7 will be “embedded” in those handles, meaning that it affects the meaning of the functions g
and Dg
.
edited Nov 22 at 15:30
answered Nov 22 at 14:35
Cris Luengo
18.2k51847
18.2k51847
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429975%2frefer-to-second-function-output-in-matlab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
The
@
symbol forms a handle to either the named function that follows the@
sign, or to the anonymous function that follows the@
sign.g=@identify
does not give you the first output but gives your functionidentify
another nameg
so you can use it as[g Dg] = g(x)
. Read more here.– Anthony
Nov 22 at 12:10
Honestly, I can read the question in 5 different ways ... and I see answers trying to answer different things. Try to explain a bit more explicitely what is it you want to achieve or to get as output, and also please do not reuse symbols names in the same code snipet (your
g
variable is used in different places with the same name while they are completely different variables with different scopes).– Hoki
Nov 22 at 18:04