How to extract one component in a vector function

3 Ansichten (letzte 30 Tage)
hung lequang
hung lequang am 4 Nov. 2015
Kommentiert: hung lequang am 4 Nov. 2015
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.

Akzeptierte Antwort

Guillaume
Guillaume am 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.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by