How do I use map with custom function in Octave?

百般思念 提交于 2019-12-08 16:25:23

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/11707464/how-do-i-use-map-with-custom-function-in-octave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!