help-octave
[Top][All Lists]

cellfun and arrayfun function

 From: jessica . gervais Subject: cellfun and arrayfun function Date: Wed, 14 Mar 2007 16:30:47 +0100

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
....

If I use a function depending only of one variable, it works ....
well, I don't kown what's wrong....
Have you any idea ?

I think there is a way to  run arrayfun with a function with many inputs
.
I don't know if the arrayfun function is already implemented in octave,here is the link of the a code developped by Bill Denney :
http://www.cae.wisc.edu/pipermail/octave-maintainers/2006-October/000805.html