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
2014년 4월 29일
No, the closest is
indexat = @(expr, index) expr(index);
F2 = @(x) indexat(a(x), 2);
Star Strider
2014년 4월 29일
편집: Star Strider
2014년 4월 29일
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
2014년 4월 29일
Star Strider
2014년 4월 29일
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
2014년 4월 29일
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에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!