如何在Octave中使用具有自定义功能的地图? [英] How do I use map with custom function in Octave?

查看:145
本文介绍了如何在Octave中使用具有自定义功能的地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个集合 A

Assume I have a collection A:

A = [0:6:100]

我有一个函数 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 

我打算在 A 上应用 fib(n),并将其存储在集合说B,其中B [i,j] 是(i,fib(i)),所以我可以绘制 vs fib(i)并在图表中查看结果。

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.

请告诉我如何使用 map 来获取此所需的集合 B

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

推荐答案

您可以这样做: b

You can do it like this:

map(@fib, A)



@ 使 fib 成为一个函数句柄。请注意, map 已被弃用,您应该使用 arrayfun 代替:

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

arrayfun(@fib, A)

这篇关于如何在Octave中使用具有自定义功能的地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆