【问题标题】:Iteratively take mean of column in Matlab在Matlab中迭代地取列的平均值
【发布时间】:2012-12-27 19:51:05
【问题描述】:

您好,我在 Matlab (PDS(:,39)) 中有一列值。此列针对各种内容进行了过滤,并且有两个单独的标记列 (PDS(:,[41 81])),对于有效行为 0,对于无效行为 -1。我正在取有效数据的平均值,如果平均值高于 0,我想让这个值无效并再次取平均值,直到平均值低于某个值(在本例中为 0.2)。这是我的代码:

% identify the VALID values
U1 = (PDS(:,81)==0);
F1 = (PDS(:,41)==0);

% only calculate using the valid elements
shearave = mean(PDS(U1&F1,39));

while shearave > 0.2
    clear im
    % determine the largest shear value overall for filtered and
    % non-flagged
    [c im] = max(PDS(U1&F1,39));
    % make this value a NaN
    PDS(im,39)=NaN;
    % filter using a specific column and the overall column
    PDS(im,41)=-1;
    F1 = (PDS(:,41)==0);
    % calculate shear ave again using new flagging column - remove the ";" so I can see        the average change
    shearave = mean(PDS(U1&F1,39))
end

Matlab 给我的输出是:

shearave =

0.3032

shearave =

0.3032

shearave =

0.3032

循环未使用新的有效数据重新评估。我该如何解决这个问题?我必须休息还是继续?或者也许是不同类型的循环?感谢您的帮助。

【问题讨论】:

    标签: matlab loops while-loop filtering vectorization


    【解决方案1】:

    您不需要使用循环,我会执行以下操作:

    对数据进行排序:

    m=PDS(U1&F1,39);
    [x isort]=sort(m); 
    

    然后计算排序后向量的累积均值:

    y = cumsum(x)./[1:numel(x)]';
    

    然后在 0.2 处截断,并使用找到的索引检索所需的值...

    ind=find(y<=0.2);
    values_needed=m(isort(ind));
    

    【讨论】:

    • 是的,我刚刚得出了同样的结论!对它进行降序排序并删除值,直到平均值低于 0.2,识别我必须通过时间戳删除的值。不过,我有一个循环——这会有所帮助。谢谢!
    【解决方案2】:

    您将第 39 列中的值反复替换为 NaN。但是,mean 不会忽略 NaN,而是返回 NaN 作为新的平均值。你可以通过一个小实验看到这一点:

    >> mean([3, 4, 2, NaN, 4, 1])
    ans = NaN
    

    因此,shearave &lt; 0.2 永远不会是 true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2016-06-30
      • 2011-02-03
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多