Recursive inline anonymous function in script

Viewed 275

I try to write a recursive inline anonymous function in a Matlab script.

Here an MWE:

funR = @(x) [x(1) funR(x(2:end))];
funR(0:5);

But this throws the following exception:

Undefined function or variable 'funR'.

This works when it's run in a function file but not when run in a script. This is because Matlab does read those differently.

My expected result from this MWE is:

[0, 1, 2, 3, 4, 5]

How to do this right?

The goal is to have funR defined as inline function, therefore a two or more line solution is not what I want. Please ignore if this or the MWE makes any sense, that is not the point of this question.

1 Answers
Related