how to convert handle to array

hello, it's my first post and i'm not very good in english, so soryy for my errors. I've got one problem with matlab. I've got a handle like a=@(x)( f1(x), f2(x),..,fn(x)) and i have to read one of function in handle to have something like this @(x)(f2(x)). is there any function to convert handle argument to structure where the functions will be in separate cells like [f1(x), f2,..fn]?

답변 (2개)

Walter Roberson
Walter Roberson 2014년 4월 29일

0 개 추천

No, the closest is
indexat = @(expr, index) expr(index);
F2 = @(x) indexat(a(x), 2);
Star Strider
Star Strider 2014년 4월 29일
편집: Star Strider 2014년 4월 29일

0 개 추천

There is no one function, but it is easy enough. Since it is not obvious from your question what the individual functions return, you are correct in assigning them to Cell Arrays.
Note the use of ‘curly brackets’ { } to define the cell arrays in the assignment to a and the cell references in y1 and y2.
Example:
f1 = @(x) x^2;
f2 = @(x) 1:x;
a = @(x) [{f1(x)} {f2(x)}];
y = a(5)
y1 = y{1}
y2 = y{2}
produces:
y =
[25] [1x5 double]
y1 =
25
y2 =
1 2 3 4 5

댓글 수: 4

Blazej
Blazej 2014년 4월 29일
we not understand. i've got n-argument handle and i have to send one of the argument to another function like a=@(x)(x(1)^2, x(2)^2, 2* x(1)) and i dont need value, but one of function to have @(x)(x(2)^2)
If all your functions are producing scalar outputs for a particular input, that is even easier. Use square brackets [ ] to create a vector (or matrix):
a=@(x) [x(1)^2, x(2)^2, 2* x(1)];
x = [3 5];
y = a(x)
produces:
y =
9 25 6
Blazej
Blazej 2014년 4월 29일
if i do it i get vector of values of funcion in handle, but i have to have all functions separately. I have process your's code and i have good answer ;) a = @(x) [{@(x) x(1)^2} {@(x) x(2)^3}];
y = a(1) y1 = y{1} y2 = y{2} but i don't know what y = a(1) do, but it works..
Star Strider
Star Strider 2014년 4월 29일
편집: Star Strider 2014년 4월 29일
The
y = a(1)
assignment passes the value 1 to the anonymous function a and creates a cell array of function evaluations. The next two lines (assignments to y1 and y2) access individual elements of the cell array.
I am happy it does what you want it to!

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

카테고리

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

태그

질문:

2014년 4월 29일

편집:

2014년 4월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by