将 Octave arrayfun 与字符串参数一起使用?
Using Octave arrayfun with a string argument?
我正在使用 arrayfun 绘制自定义函数的结果,该函数执行一些逻辑、查找和计算。我原来的电话看起来与此类似:
plot(x, arrayfun(@Q, x.^2, someNumericVariable));
效果很好。不过除了someNumericVariable这个参数,我还想加一个参数someStringVariable,所以改成这样:
plot(x, arrayfun(@Q, x.^2, someNumericVariable, someStringVariable));
但是,当尝试使用它时,出现错误:
error: arrayfun: dimensions mismatch
我猜这是由于 GNU Octave 文档中的这一行造成的:
If given more than one array input argument then all input arguments must have the same sizes
(https://www.gnu.org/software/octave/doc/interpreter/Function-Application.html)
所以我假设我试图传递的字符串被视为一个数组,它的维度与数字常量值不同?
如果是这样,在保持代码语法简洁的同时,我是否可以采取任何解决方法?
一种适用于 MATLAB 的解决方法是:
plot(x, arrayfun(@(u,v) Q(u,v,someStringVariable), x.^2, someNumericVariable));
我正在使用 arrayfun 绘制自定义函数的结果,该函数执行一些逻辑、查找和计算。我原来的电话看起来与此类似:
plot(x, arrayfun(@Q, x.^2, someNumericVariable));
效果很好。不过除了someNumericVariable这个参数,我还想加一个参数someStringVariable,所以改成这样:
plot(x, arrayfun(@Q, x.^2, someNumericVariable, someStringVariable));
但是,当尝试使用它时,出现错误:
error: arrayfun: dimensions mismatch
我猜这是由于 GNU Octave 文档中的这一行造成的:
If given more than one array input argument then all input arguments must have the same sizes (https://www.gnu.org/software/octave/doc/interpreter/Function-Application.html)
所以我假设我试图传递的字符串被视为一个数组,它的维度与数字常量值不同?
如果是这样,在保持代码语法简洁的同时,我是否可以采取任何解决方法?
一种适用于 MATLAB 的解决方法是:
plot(x, arrayfun(@(u,v) Q(u,v,someStringVariable), x.^2, someNumericVariable));