2013-01-13 75 views
1

嗨我有一列值的Matlab(PDS(:,39))。此列針對各種情況進行了過濾,並且有兩個分隔標記列(PDS(:,[41 81])),它們對於有效行是0或對於無效行是-1。我正在考慮有效數據的均值,如果均值大於0,我想使這個值無效,並再次採用均值,直到均值低於某個值(在本例中爲0.2)。這裏是我的代碼:迭代地取平均值在Matlab列

% 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

迴路不與新的有效數據重新evalulating。我該如何解決這個問題?我必須使用休息還是繼續?或者也許是一種不同類型的循環?謝謝你的幫助。

回答

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

+1,非常聰明! –

+0

是的,我剛剛得出同樣的結論!對它進行降序排序並刪除值,直到平均值低於0.2,根據時間戳識別我必須刪除的值。我雖然有一個循環 - 這會有所幫助。謝謝! – user1854628

0

您反覆將第39列中的值替換爲NaN。然而,mean不會忽略NaN,而是返回NaN作爲新的平均值。你可以用一個小實驗看到這一點:

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

因此,shearave < 0.2永遠不會true