MATLAB: How to extract one component in a vector function

functionMATLABvector

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.

Best Answer

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.