【问题标题】:How to calculate the standard deviation for every 100 points in a nx3 vector?如何计算nx3向量中每100个点的标准差?
【发布时间】:2013-07-16 09:39:31
【问题描述】:

假设我有一个包含 n 行的矩阵,每行包含三个坐标(x、y 和 z)。我想计算 MATLAB 中每 100 个点的标准差。例如,对于前 100 个 x 坐标,我应用 std,然后对 y 和 z 坐标应用相同,依此类推……最终,我每 100 个点都有一组 x、y 和 z 值。我该怎么做?

【问题讨论】:

  • 你能发布一些代码来展示你到目前为止所做的尝试吗?
  • 我觉得我可能误解了您的矩阵的大小,但无论哪种方式,根据我的回答得到一个可行的解决方案应该不会太难。
  • 所以有很多数据,我可以尝试单独运行它。因此,例如第一组大约有 3000 组点。
  • 我在想一些类似使用 reshape 或 ndgrid 的东西?

标签: matlab standard-deviation


【解决方案1】:

我会这样做:

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

【讨论】:

  • 非常感谢!最后一个问题-要将所有这些存储在一个新向量中,我应该在哪里包含它?假设我想要一个 3 x n 向量中的所有内容?或者如果我想取每一行的平均值来产生一个 1 x n 向量?
  • 我已经发布了一个修改后的解决方案
  • @LuisMendo 您应该考虑将新解决方案编辑到此答案中,而不是将其作为单独的解决方案发布。
  • +1 挑战for 循环禁忌以获得可靠的可读答案。开始溢出的好方法!
猜你喜欢
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 2019-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多