简体   繁体   English

Matlab / octave中的映射功能,如LISP中的mapcar

[英]mapping function in matlab / octave like mapcar in LISP

I am looking to find mapping functions in matlab. 我正在寻找在matlab中查找映射函数。 So here is the example I'm thinking of: 因此,这是我正在考虑的示例:

kvec = 0:1:16
kvec =

 0     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16

Now I wish to produce a second vector called hvec which essentially computes 10 ^ -k for each value in the kvec. 现在,我希望生成一个称为hvec的第二个向量,该向量实质上为kvec中的每个值计算10 ^ -k。

Is there an elegant way to do that? 有没有一种优雅的方法可以做到这一点?

Thanks in advance. 提前致谢。

I guess what you need is 我想你需要的是

kvec = 0:1:16;
hvec = 10 .^ -kvec

arrayfun , cellfun , spfun , structfun and bsxfun are some useful mapping functions in MATLAB. arrayfuncellfunspfunstructfunbsxfun是MATLAB中一些有用的映射函数。

For example using arrayfun hvec would be computed this way: 例如,使用arrayfun hvec可以通过以下方式计算:

hvec = arrayfun(@(x) 10^-x, kvec)

This syntax is obviously longer than using array power .^ , but as arrayfun is a mapping function it works for any given function, whereas there are only a few array functions available (whose operators' first character is . such as .^ , .* etc.). 这种语法显然比使用数组幂.^更长,但是由于arrayfun映射函数,因此它可用于任何给定函数,而只有少数数组函数可用(其运算符的第一个字符为.例如.^.*等等。)。

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

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