Anonymous functions with 2 ou more outputs?

Hi,
I would like to know if it's possible to program an anonymous function with more than one output WITHOUT using another already defined function that have more than 1 output (I know this is actually possible). I need to program an anonymous function which takes a matriz with P+Q columns and "breaks" it in 2 matrixes: A and B, where A has P columns and B has Q columns. So the function output would be [A, B]. It'd really help me.
Thanks in advance, Guilherme

댓글 수: 1

bym
bym 2011년 7월 15일
do you really need it to be an anonymous function?

댓글을 달려면 로그인하십시오.

 채택된 답변

Paulo Silva
Paulo Silva 2011년 7월 15일

10 개 추천

m=[1 2;3 4;5 6]
fcol=@(x)deal(x(:,1),x(:,2))
[a b]=fcol(m)
In this simple case you only got 2 columns but you can expand it to multiple columns.
m=[1 2 3 4;5 6 7 8;9 10 11 12]
fcol=@(x)deal(x(:,1:2),x(:,3:4))
[a b]=fcol(m)

댓글 수: 4

Guilherme
Guilherme 2011년 7월 15일
What an elegant solution Sr.Paulo! Thanks a lot! =)
Oleg Komarov
Oleg Komarov 2011년 7월 15일
Agree
Paulo Silva
Paulo Silva 2011년 7월 15일
You don't even need the anonymous function
m=[1 2 3 4;5 6 7 8;9 10 11 12]
[a b]=deal(m(:,1:2),m(:,3:4))
Thank you for this elegant solution!!!
In combination with tables and rowfun more than helpful

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Oleg Komarov
Oleg Komarov 2011년 7월 15일

0 개 추천

[f,g] = @(x) x+1
??? Error: Only functions can return multiple values.
If you post some code we could try to find a different solution.
Also, does this help?:
A = rand(10,10);
A = mat2cell(A,10,[5 5]);
[B,C] = A{:};

댓글 수: 4

Paulo Silva
Paulo Silva 2011년 7월 15일
Oleg just use the deal function :)
Oleg Komarov
Oleg Komarov 2011년 7월 15일
I never use it...
Francois
Francois 2018년 12월 1일
After many years of frustration with the fact that Matlab does not allow multiple outputs from anonymous functions, I have finally come up with a solution. It's not elegant but it works. I have created a function called mydeal.m which I put in my matlab directory
function varargout = mydeal(x)
for k = 1:length(x)
varargout{k} = x{k};
end
Now when I want an inline function with multiple outputs I just wrap a cellarray of outpus in mydeal():
Here is a simple example showing how to create an inline function that returns the a vector valued function, its gradient, and its hessian:
>> x0 = [1;1;1]; W = diag([2;4;16]);
>> f = @(x) mydeal( { 0.5*(x-x0)'*W*(x-x0), (x-x0)'*W, W } );
>> [f0,g0,H0] = f([1;2;3])
f0 =
34
g0 =
0 4 32
H0 =
2 0 0
0 4 0
0 0 16
Now if only mydeal.m could be made part of the standard matlab libraries then I could create simple inline examples in the help block of my functions that take in as input functions with multiple outputs.
JM
JM 2019년 10월 24일
편집: JM 2019년 10월 24일
Francois, you can use deal() for that, like Paulo Silva answered:
>> x0 = [1;1;1]; W = diag([2;4;16]);
>> f = @(x) deal(0.5*(x-x0)'*W*(x-x0), (x-x0)'*W, W);
>> [f0,g0,H0] = f([1;2;3])

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

질문:

2011년 7월 15일

댓글:

2021년 3월 31일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by