1

I cannot find this syntax anywhere

y = @(beta, x)x*beta;

where x is some vector or matrix lets say. Is @ used to reference another function so you can have more than one externally visiuble function in the same .m file? Sorry I'm new to matlab but can't find this in the documentation

marsei
  • 7,691
  • 3
  • 32
  • 41
Luke Makk
  • 1,157
  • 3
  • 13
  • 13
  • The function handle help page is [here](http://www.mathworks.com/help/matlab/ref/function_handle.html). See in particular the exemple: 'sqr = @(x) x.^2;' Then 'a = sqr(5)' gives a = 25. – marsei Aug 07 '13 at 21:06
  • see `doc function_handle`: http://www.mathworks.com/help/matlab/function-handles.html. You could have found this out by `help @` and looking through the list of special characters for `@` – Amro Aug 07 '13 at 21:08
  • I don't understand why this question is voted negatively. `@ matlab` search in google gives nothing interesting, and typing `@` in the documentation leave the result page blank. – marsei Aug 07 '13 at 21:16
  • 1
    `help @` and `doc @` in Matlab itself both work fine and indicate that the "at" symbol is used for "function handle creation". Also `at matlab` in Google returns [this page](http://www.mathworks.com/help/matlab/ref/specialcharacters.html) as the first hit (`@ matalab` is just poor Google skills unless you're looking for a Twitter handle). – horchler Aug 07 '13 at 21:21

2 Answers2

4

That is the way to define an anonymous function in Matlab. It is basically the same as

function result = y(beta, x)

result = x * beta;

but without needing an m-file or subfunction to define it. They can be constructed within other m-files or even within an expression. Typical use is as a throw-away function inside the call of some complex function that needs a function as one of its inputs, i.e.:

>> arrayfun(@(x) x^2, 1:10)

ans =

     1     4     9    16    25    36    49    64    81   100

I personally use them a lot to refactor a list of repetitive statements

a = complex_expression(x, y, z, 1)
b = complex_expression(x, y, z, 3)
c = complex_expression(x, y, z, 8)

into

f = @(n) complex_expression(x, y, z, n)
a = f(1)
b = f(3)
c = f(8)

More info from the Mathworks. They are more or less the same as a lambda expression in Python.

Lokesh A. R.
  • 2,326
  • 1
  • 24
  • 28
Bas Swinckels
  • 18,095
  • 3
  • 45
  • 62
  • is it ok for this to happen at the very beginning of a function where beta is not even define yet? can this be referenced later on – Luke Makk Aug 07 '13 at 21:31
  • beta is one of the parameters of the anonymous function, exactly the same as when you would define it with `function result = y(beta, x)`, so beta gets replaced by whatever value you call the function with, so when you call `y(5, 10)`, beta will get the value 5 and the result will be calculated. – Bas Swinckels Aug 07 '13 at 21:36
0

Think of @ as anonymous which means an unnamed function (technically, in MATLAB, you must give it a name since you cannot do e.g., (@(x, y) x + y)(1, 2), that name is whatever variable you assign the anonymous function to).

The syntax @(x, y) x + y reads: create an anonymous @ function with parameters x and y and return the result of the evaluation of the expression following the next closing parenthesis. In this case, that's the addition of x and y.

One thing that almost no one I know who uses MATLAB uses on a regular basis and instead uses repmat hell instead is bsxfun (it stands for binary singleton expansion).

With the expression bsxfun(@plus, randn(1000, 20), randn(1000, 1)) you essentially repmat the right hand side 20 times to form a new matrix that you then pass to the @plus function handle. You can pass any function around this way. Take a look at cellfun and arrayfun. They are incredibly useful.

Phillip Cloud
  • 24,919
  • 11
  • 68
  • 88