Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 将常数函数矢量化为倍频程_Matlab_Octave - Fatal编程技术网

Matlab 将常数函数矢量化为倍频程

Matlab 将常数函数矢量化为倍频程,matlab,octave,Matlab,Octave,我在定义一个以向量为参数的常数函数时遇到了一个存在问题: 我想定义一个函数,如: >f=@(x) 0.0; % a constant function (zero or any other constant value). >xx=linspace(0,10,10); % ten values where I want to evaluate the function 当我运行它时,我得到: >> f(xx) ans = 0 我真的期待一个零向量。

我在定义一个以向量为参数的常数函数时遇到了一个存在问题:

我想定义一个函数,如:

>f=@(x) 0.0;           % a constant function (zero or any other constant value).
>xx=linspace(0,10,10); % ten values where I want to evaluate the function
当我运行它时,我得到:

>> f(xx)
ans = 0
我真的期待一个零向量。(我不知道如何将矢量化功能合并到常量函数中)

有人能解决这个简单的问题吗?
提前谢谢

要根据输入形状重复常量
a
,可以使用

f = @(x) repmat(a, size(x));
例如:

>> a = 5;
>> f = @(x) repmat(a,size(x));
>> f([10 20 30; 40 50 60])
ans =
     5     5     5
     5     5     5

好的,从上面的注释中,您需要一个函数,它将返回一个与输入大小相同的数组,但所有值都等于某个预定义常量

@路易斯的上述方法将奏效。现在我不太清楚Matlab,但至少在旧版本和倍频程中,repmat并不是最快的,而且还有其他生成重复数组的低开销方法。如果这是一个可能被多次调用的函数,我更喜欢使用
ones()
array expansion。(它还与广播结合得很好,在这种情况下这不是必要的)

例如:

通过快速而肮脏的循环来估计时间需求,我们发现在Octave 5.2.0中,repmat版本在相同的操作中花费的时间大约要长5倍:

>> tic;for idx = 1:1e4, f(rand(4,2,3));endfor,toc
Elapsed time is 1.54 seconds.

>> tic;for idx = 1:1e5, f(rand(4,2,3));endfor,toc
Elapsed time is 17.6487 seconds.

>> tic;for idx = 1:1e4, g(rand(4,2,3));endfor,toc
Elapsed time is 0.337171 seconds.

>> tic;for idx = 1:1e5, g(rand(4,2,3));endfor,toc
Elapsed time is 3.06284 seconds.

仅供参考,如果在倍频程中工作,我强烈推荐使用这一点,因为它目前没有Matlab近年来实现的许多代码加速。

我真的希望得到一个零向量
f=@(x)0.0
定义一个只输出
0
的函数。
f=@(x)零(大小(x))
你想要什么?或者
@(x)x*0
也可以。如果在函数中不使用
x
,则其输出独立于输入。您是否希望依次为输入的每个元素调用函数?八度音程不是这样工作的。好吧,路易斯蒙多在那个cse中,它只对零值常量函数有效,但对,比如说,f=@(x)3.0,rigth?不起作用?。我希望输出3。。。我认为可以设置为:f=@(x)a*ones(1,size(x))@CrisLuengo@(x)x*0也不适用于另一个0的常数。“您是否希望依次为输入的每个元素调用函数?”。我想这就是我想要的!。所以,也许你想要的是
f=@(x)repmat(a,size(x))
。我真的没有得到你想要的,抱歉,说得好。在MatlabR201B中,它们似乎同样快
>> tic;for idx = 1:1e4, f(rand(4,2,3));endfor,toc
Elapsed time is 1.54 seconds.

>> tic;for idx = 1:1e5, f(rand(4,2,3));endfor,toc
Elapsed time is 17.6487 seconds.

>> tic;for idx = 1:1e4, g(rand(4,2,3));endfor,toc
Elapsed time is 0.337171 seconds.

>> tic;for idx = 1:1e5, g(rand(4,2,3));endfor,toc
Elapsed time is 3.06284 seconds.