help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Numerical Differentiation and Integration of Array Data


From: Przemek Klosowski
Subject: Re: Numerical Differentiation and Integration of Array Data
Date: Wed, 07 Dec 2011 09:03:24 -0500
User-agent: Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20110930 Thunderbird/7.0.1

On 12/06/2011 10:38 PM, syberraith wrote:
Even this fails to work:

a = 2;
b = 3;
one = @(x) a.*x + b;
two = @(x) a.^2.*x + b;
one(4)
two(5)

error: `a' undefined near line 3 column 12

In Octave you have to explicitly declare global variables to be able to see them across the 'lexical scopes' of different function definitions. Anonymous functions don't magically exempt you from that---but they can capture the values of variables from the scope they are defined in, at the time they are defined in.

See again what Carlo was showing; adapted to your example, it'd be

a=2;b=3;
one = @(x,a,b) a.*x+b
one_specialized_for_ab = @(x) one(x,a,b)
one_specialized_for_ab(10)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]