1

If I have an anonymous function in MATLAB, say

f = @(t) [t, t+5];

and I wanted to create a new anonymous function g that accesses the first index of f, so that, in this example,

g = @(t) t;

how can I do so?

I have tried doing something along the lines of

g = @(t) f(t)(1);

but the compiler says that this is "invalid array indexing", even though I thought that this would basically allow g to be g(t) = t

Adriaan
  • 17,741
  • 7
  • 42
  • 75
ShonenMind
  • 31
  • 2
  • What about `g = f(1)`? `f` appears to be an array of function handles, logically I'd assume you simply index into that – Adriaan Apr 19 '23 at 10:22
  • 1
    Unfortunately. I believe that g = f(1) actually gives the array [1, 6] to g. So essentially, g = f(1) means g = [1, 1 + 5] – ShonenMind Apr 19 '23 at 10:26
  • 1
    I _think_ this is a duplicate of this slightly more generic question: https://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it and your answer is basically [Vugar's answer](https://stackoverflow.com/a/16322659/3978545) – Wolfie Apr 19 '23 at 10:51

1 Answers1

2

I believe I have resolved this. One can do the following:

f = @(t) [t, t+5];

first = @(t) t(1);

next = @(t) first(f(t));

ShonenMind
  • 31
  • 2