Matlab的std在REPL中工作但在程序中不起作用

mil*_*lli 7 matlab

我想计算矩阵元素的标准推导.所以我首先用命令将我的矩阵转换reshape为矢量,然后使用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);
Run Code Online (Sandbox Code Playgroud)

所以我B在传递之前打印了我的矢量std.我把它分配给xREPL中的一个变量尝试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
Run Code Online (Sandbox Code Playgroud)

这是向量B,就在错误消息之前:

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
Run Code Online (Sandbox Code Playgroud)

ang*_*nor 6

很可能你的B矢量是某种int类型.试着这样打电话

std(double(B))
Run Code Online (Sandbox Code Playgroud)

上面的语句首先转换B为double类型,然后调用std.

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