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
用MATLAB实现标准差_Matlab - Fatal编程技术网

用MATLAB实现标准差

用MATLAB实现标准差,matlab,Matlab,我试图在MATLAB中使用公式计算标准偏差 for i=1:n s=sqrt(sum((h(i)-mean(h))^2)/(n-1)); end 其中,n是单个列向量中的行数,但结果与通过std(h)计算的结果不同。在我的项目中,我不能使用std函数 请帮助我。在我看来,您最好不要使用for循环,而是使用矢量化代码 s1 = sqrt(sum((h - mean(h)).^2)./(n-1)) 这里,sum负责for循环完成的求和 如果您确实想使用for循环,您需要在循环中添加每个单独的项

我试图在MATLAB中使用公式计算标准偏差

for i=1:n 
s=sqrt(sum((h(i)-mean(h))^2)/(n-1));
end
其中,
n
是单个列向量中的行数,但结果与通过
std(h)
计算的结果不同。在我的项目中,我不能使用
std函数


请帮助我。

在我看来,您最好不要使用for循环,而是使用矢量化代码

s1 = sqrt(sum((h - mean(h)).^2)./(n-1))
这里,
sum
负责for循环完成的求和

如果您确实想使用for循环,您需要在循环中添加每个单独的项,然后取其平方根;i、 e.不要在循环内使用
sum

clc
clear

h = rand(1,100);
M = mean(h);

n = length(h);

s0 = 0; %// Initialize s0, the standard deviation you wish to calculate.
for i=1:n     
    s0 = s0 + (h(i)- M)^2; %// add each calculated s0 to its previous value. That's the sum.
end

s0 = sqrt(s0/(n-1))

%// Calculate values using vectorized code of Matlab std function.
s1 = sqrt(sum((h - mean(h)).^2)./(n-1))
s2 = std(h)
检查s0、s1和s2:

s0 =

    0.2842


s1 =

    0.2842


s2 =

    0.2842

我不知道Matlab,但唯一的循环应该是所有(数据平均值)^2的总和。
不存在与/(n-1)或sqrt()的循环

对,看起来你有点像s.d=

但你想让它匹配


因此,对求和得到的变量执行/(n-1)和平方根运算。

Awesome!很乐意帮忙:P