在Matlab中迭代取列的意思

时间:2013-01-13 21:44:05

标签: matlab loops while-loop filtering vectorization

嗨我在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

循环不会使用新的有效数据重新进行重新评估。我该如何解决这个问题?我必须休息或继续吗?或者也许是一种不同类型的循环?谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

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

对数据进行排序:

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));

答案 1 :(得分:0)

您使用NaN迭代地将第39列中的值替换为mean。但是,NaN不会忽略NaN,而是返回>> mean([3, 4, 2, NaN, 4, 1]) ans = NaN 作为新的平均值。您可以通过一些小实验来看到这一点:

shearave < 0.2

因此,true永远不会是{{1}}。