So our postdoctural machine learner Mikkel N. Schmidt noted the inadequacies of programming languages. For the following code

> a = [1; 2]
> b = [1; 2]
> x = a'*b
> a = [3; 4]

he would like to have the ‘x’ updated with the new ‘a’ so the result would be ’11’. In vanilla Matlab and Python you end up with the old value of ‘x’ based on the initial computation: ‘5’.

Mikkel pointed to the programming language Haskell, where this functionality supposedly would be possible. I am not into this functional programming thing but I have noticed there is this ‘lambda’ word in the Python programming language, and it seems to get you some of the way:

But you get the result ‘5’. This is not a bug but a feature of Matlab: Upon construction of the function handle Matlab captures the current value of variables that are not among the input arguments and it stores them throughout the lifetime of the function handle. I wonder if there is another way to do it. You could define a function in a separate file with global variables.

One thought on “Vector multiplication with some delay and intelligence: anonymous functions in Matlab and Python”

Mikkel N. Schmidt said:October 6, 2010 at 10:51 am

In Matlab we could also writex = @() evalin(‘caller’,’a’)’*evalin(‘caller’,’b’);Not pretty, but it does the trick. What I would really like to have is a language where this is all natural and easy to read/write.