简体   繁体   English

如何在Octave中使用带自定义功能的地图?

[英]How do I use map with custom function in Octave?

Assume I have a collection A : 假设我有一个集合A

A = [0:6:100]

And I have a function fib(n) : 我有一个函数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. 我打算能够在A上应用fib(n) ,并将其存储在B集合中,其中B [i,j](i,fib(i)) ,所以我可以绘制i vs fib(i)并在图表上查看结果。

Please advise on how I can use map to obtain this desired collection B . 请告知我如何使用地图来获得所需的收藏品B.

You can do it like this: 你可以这样做:

map(@fib, A)

The @ makes fib into a function handle. @使fib成为一个函数句柄。 Note that map is being deprecated and you should use arrayfun instead: 需要注意的是map中被取消,你应该使用arrayfun改为:

arrayfun(@fib, A)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM