help-octave
[Top][All Lists]

## Re: cellfun and arrayfun function

 From: David Bateman Subject: Re: cellfun and arrayfun function Date: Mon, 19 Mar 2007 10:55:20 +0100 User-agent: Thunderbird 1.5.0.7 (X11/20060921)

```address@hidden wrote:
> Hi ,
>
>
> I am currently writing a octave (octave2.9) code and I would like to
> use the arrayfun and cellfun function to apply a function depending on
> several input on each elements of an array or on each elements of a cell.
>
> I read the Workmath help pages afor cellfun and arrayfun. They say :
> "The first input argument fun is a function handle to a function that
> takes one input argument and returns a scalar value". Does that mean
> that the function fun has to take only ONE input argument and return a
> scalar ?
>
> If I have a function t=f(a,b,c) and that I ant to aplly it on several
> arrays A,B and C (A representing the variable a, B  representing the
> variable b, C epresenting the variable c), and get the result in an
> array T ? is it possible ? ... Well, I tried it but it seems that it
> doesn't work ...but I don't know if it's a problem of syntax or not...
>
> (here is a simple exemple I've tested
>
> "simple is a function saved in the simple.m file"
>
>     function[z]=simple(x,y)
>     z=2*x+y;
>     end
>
>
> octave2.9:1> A=[1,2;3,4]
> A =
>
>   1  2
>   3  4
>
> octave2.9:2> simple(2,3)
> ans =  7
> octave2.9:3> B=arrayfun(@simple,A,A)
> error: `y' undefined near line 2 column 7
> error: evaluating binary operator `+' near line 2, column 6
> error: evaluating assignment expression near line 2, column 2
> error: called from `simple' in file
> `/home/gervais/2007/Noise/interface/simple.m'
> error: called from `arrayfun' in file
> `/usr/share/octave/2.9.6/m/miscellaneous/arrayfun.m'
> error: evaluating assignment expression near line 3, column 2
Where did your arrayfun function come from. Its not in the octave CVS
even in the Attic. Bill Denney in October sent this e-mail

http://www.cae.wisc.edu/pipermail/octave-maintainers/2006-October/000805.html

with an implementation, but it was never committed to the CVS (John can
we commit this function?). In any case, you should use it differently
than you have. Try

fun = @(x,y) 2*x+y
A = [1,2;3,4];
B = arrayfun(fun,A,A)

though there appears to be a bug in cellfun in the CVS as B does not
have the same shape as A.

Regards
David

--
Motorola Labs - Paris                        +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin    +33 6 72 01 06 33 (Mob)
91193 Gif-Sur-Yvette FRANCE                  +33 1 69 35 77 01 (Fax)

The information contained in this communication has been classified as: