How to extract one component in a vector function
3 views (last 30 days)
Show older comments
I have a vector whose 2 components are functions of x as follows
v = @(x) [x^2 ; x^2]
and I want now to introduce the new function h(x) which is defined as the first component of the vector v.
My question is how to define the function h(x). Thank you in advance.
0 Comments
Accepted Answer
Guillaume
on 4 Nov 2015
If you want to use an anonymous function as you've done for v, you run into the limit of matlab languages, you cannot indeed do:
h = @(x) v(x)(1)
as matlab won't let you chain () expressions.
However, indexing is implemented by subsref, so you can call subsref directly. It makes for one ugly syntax though:
h = @(x) subsref(v(x), struct('type', '()', 'subs', {{1}})); %will return 1st element of v(x)
Note the double wrapping of the index {{1}} into a cell array required by struct.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!