Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use map with custom function in Octave?

Assume I have a collection A:

A = [0:6:100]

And I have a function fib(n):

function retval=fib(n)
   g1=(1+5^.5)/2
   g2=(1-5^.5)/2
   retval=(1/5^.5)*(g1^n - g2^n)
endfunction 

I intend to be able to apply fib(n) on A, and store it in a collection say B, where B[i,j] is (i,fib(i)), so I can plot i vs fib(i) and see the results on a graph.

Please advise on how I can use map to obtain this desired collection B.

like image 638
JWL Avatar asked Jul 29 '12 07:07

JWL


1 Answers

You can do it like this:

map(@fib, A)

The @ makes fib into a function handle. Note that map is being deprecated and you should use arrayfun instead:

arrayfun(@fib, A)
like image 98
Thor Avatar answered Sep 27 '22 20:09

Thor