假设我有一个 n 行的矩阵,每个行包含三个坐标,(x,y和z)。我想计算MATLAB中每100个点的标准偏差。例如,对于前100个坐标,我应用std
,然后对y和z坐标相同,依此类推......最终我每100个点有一组x,y和z值。我该怎么做?
答案 0 :(得分:2)
我会这样做:
M = randn(120,3); % substitute this for the actual data; 3 columns
N = 100; % number of elements in each set for which std is computed
cols = size(A,1);
for n = 1:ceil(cols/N)
row_ini = (n-1)*N+1;
row_fin = min(n*N, cols); % the "min" is in case cols is not a multiple of N
std(A(row_ini:row_fin,:))
end
如果考虑速度,“for”循环可能会被矢量化。
编辑:如果要将所有结果存储在三列矩阵中,只需修改“std”行并添加一些初始化,如下所示:
M = randn(120,3); % substitute this for the actual data; 3 columns
N = 100; % number of elements in each set for which std is computed
cols = size(A,1);
n_max = ceil(cols/N);
result = repmat(NaN,ceil(cols/N),3); % initialize
for n = 1:n_max
row_ini = (n-1)*N+1;
row_fin = min(n*N, cols); % the "min" is in case cols is not a multiple of N
result(n,:) = std(A(row_ini:row_fin,:));
end