Question

I do not understand the anonymous function in the following code:

x = 0.25 * randn(3, 1);
y = 0.25 * randn(3, 1);
h = 0.1*randn(3, 1);

interpolate = @(x, y, h, x_new, y_new) ...
    feval(@(int) int(x_new, y_new), ...
          TriScatteredInterp([-1; -1; 1; 1; x], ...
                             [-1; 1; -1; 1; y], ...
                             [0; 0; 0; 0; h]));

I have some understanding about anonymous functions and the feval function, but I searched the matlab docs and do not find an example using several @ signs. Also the feval parameter have anonymous function.

Can anyone give some hints about this?

Was it helpful?

Solution

So you've probably worked out that this is creating a anonymous function with the handle interpolate:

interpolate = @(x, y, h, x_new, y_new)...

interpolate takes those five inputs, and calls feval. Now here it gets a bit tricky, because feval itself contains another anonymous function.

@(int) int(x_new, y_new), means, take input int and return the output of int(x_new,y_new). The additional input to feval, in this case TriScatteredInterp, is taken as the input to that anonymous function. This is not a reference to a built-in function int (as might be the case if you saw something starting feval(@int...).

So what interpolate does is basically equivalent to doing this, for any given set of inputs:

tsi =  TriScatteredInterp([-1; -1; 1; 1; x], ...
                             [-1; 1; -1; 1; y], ...
                             [0; 0; 0; 0; h]));

tsi(x_new,y_new)

You can test this by comparing the output of tsi(x_new,y_new) with the output of interpolate(x, y, h, x_new, y_new).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top