我想计算矩阵元素的标准差。所以我首先使用命令 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);
所以我在将它传递给 std
之前打印了我的向量 B
。我在 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
,就在错误消息之前:
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类型,然后调用std。
要检查变量的类型是什么,请在命令提示符下键入 whos
。
关于Matlab std 在 REPL 中有效,但在程序中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12731077/