help-octave
[Top][All Lists]
Advanced

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

Re: mapping a scalar function over an array


From: John Smith
Subject: Re: mapping a scalar function over an array
Date: Tue, 7 Mar 2000 19:24:21 +0000 (GMT)


If you have access to the source of foo, then you could
change the start of it to read:

  function a=foo(x)
    if (length(x) > 1)
      a = zeros(size(x)) ;
      for ix = 1:rows(x)
        for iy = 1:columns(x)
           a(ix,iy) = foo(x(ix,iy)) ;
        endfor
      endfor
      return ;
    endif

    ...
    ... rest of foo.m
endfunction

So that foo.m works for both scalar and matrix arguments.

If for that is not feasible or desirable, I guess you could have
a function:

function a = mapper( fn, x )
   a = zeros(size(x)) ;
   for ix = 1:rows(x)
     for iy = 1:columns(x)
        a(ix,iy) = feval( fn, x(ix,iy)) ;
     endfor
   endfor
   return ;
endfunction ;

##
## not a good way to evaluate sin, but legal:
##
mapper( "sin", linspace(0,pi,100))

You can do a lot of cunning things with eval(sprintf("magic command"));
and feval(...);

Just my thoughts,

  John

On Tue, 7 Mar 2000, Giulio Bottazzi wrote:

> Dear Octave users,
> I've not found in the Octave manual (probably for my stupidity) a way of
> "mapping" a scalar function over an array. What I mean is: given a function
> defined for a (truly) scalar argument, for instance
> 
> function foo(x)
>       .
>       .
>       integrate a given function between 0 and x 
>       .
> endfunction
> 
> and an array 
> 
> A=[a1,...,aN],
> 
> is there a way to obtain the array of results
> 
> B=[foo(a1),...,foo(aN)] 
> 
> with a single simple command? Something like: Map(foo,A)? ( this is approx. 
> the
> syntax I use on Mathematica).
> 
> Please, point me to the correct place in the manual if the answer is there and
> forgive me for my silliness.
> 
> Thank you,
>                       G.B.
> 
> 
> 
> -----------------------------------------------------------------------
> Octave is freely available under the terms of the GNU GPL.
> 
> Octave's home on the web:  http://www.che.wisc.edu/octave/octave.html
> How to fund new projects:  http://www.che.wisc.edu/octave/funding.html
> Subscription information:  http://www.che.wisc.edu/octave/archive.html
> -----------------------------------------------------------------------
> 



-----------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.che.wisc.edu/octave/octave.html
How to fund new projects:  http://www.che.wisc.edu/octave/funding.html
Subscription information:  http://www.che.wisc.edu/octave/archive.html
-----------------------------------------------------------------------



reply via email to

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