Matlab';s std在REPL中工作,但在程序中不工作

Matlab';s std在REPL中工作,但在程序中不工作,matlab,Matlab,我想计算矩阵元素的标准导数。因此,我首先使用命令restrape将矩阵转换为向量,然后使用std 但是,我收到一条错误消息: Error using var (line 59) First argument must be single or double. Error in std (line 32) y = sqrt(var(varargin{:})); Error in reducenoise2>standabw (line 112) s = std(B)

我想计算矩阵元素的标准导数。因此,我首先使用命令
restrape
将矩阵转换为向量,然后使用
std

但是,我收到一条错误消息:

Error using var (line 59)
First argument must be single or double.

Error in std (line 32)
y = sqrt(var(varargin{:}));

Error in reducenoise2>standabw (line 112)
            s = std(B);

Error in reducenoise2 (line 36)
 D = standabw(n,m,r,fu,D);
B =

    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    0
    0
    0
    0
    0
    0
    0
所以我打印了我的向量
B
,就在将它传递到
std
之前。我在REPL中将其分配给变量
x
,并尝试手动调用
std(x)

有趣的是,这很管用

那么,当在我的代码中使用函数
std
(使用相同的参数调用)时,如何导致错误,但在REPL中工作正常

以下是Matlab函数:

function [D] = standabw(n,m,r,fu,D)
    for i = 1+r:n-r
        for j = 1+r:m-r
            C = D(i-r:i+r,j-r:j+r);
            B = reshape(C,(2*r+1)^2,1)
            s = std(B);
            if s > fu
                D(i,j) = 255;
            end
        end
    end
end
这是矢量
B
,就在错误消息之前:

Error using var (line 59)
First argument must be single or double.

Error in std (line 32)
y = sqrt(var(varargin{:}));

Error in reducenoise2>standabw (line 112)
            s = std(B);

Error in reducenoise2 (line 36)
 D = standabw(n,m,r,fu,D);
B =

    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    0
    0
    0
    0
    0
    0
    0

很可能你的B向量是某种int类型的。试着这样称呼

std(double(B))
上面的语句首先将
B
强制转换为double-type,然后调用std


要检查,命令提示符下的变量类型是什么类型的
whos

非常感谢!现在它起作用了。但我还是不明白为什么。你能解释一下吗?很简单:它需要双倍的输入。