2017-03-07 842 views
0

我有一個通道,現在我可以從中抽取大約100萬個樣本,其中包含正數值和負數值。我的意圖是找到連續的正整數和負整數(不一定是相同的),一旦找到值,我就可以對它進行一些操作。下面給出了我的代碼。 chA是我從哪裏獲得我的輸入作爲值的渠道。該代碼只給了我43.2600的值,理想情況下應該給出一個數組,因爲有很多樣本是連續的正面和負面的。使用matlab查找整個數組的連續正數和負數元素

consider the array as [0,1,-3,4,5,6,7,8,9,-19] 
for i = 1:1000000 % loops strats from 1 and ends at 1000000 
if (chA(i)<0) && (chA((i+1) >0)) % if i = 1, i+1 = -3 <it satisfy the condition> 
tan = ((chA(i+1))- chA(i)); %calculate it 
deltaOfTime = tan/i; %store the value here in the vector deltaOfTime 
end 
now in the next iteration it should be able to find out the next consecutive positive and negative value which is 9,-19 

回答

0

我想這是你正在試圖做...

origVec=[0,1,-3,4,5,6,7,8,9,-19]; 

yTemp=origVec(:); %make a column vector 
yTemp = [NaN; yTemp; NaN]; %NaN pad 

iTemp = (1:numel(yTemp)).'; %Get index array 

% keep only the first of any adjacent pairs of equal values (including NaN). 
yFinite = ~isnan(yTemp); 
iNeq = [true;((yTemp(1:end-1) ~= yTemp(2:end)) & ... 
(yFinite(1:end-1) | yFinite(2:end)))]; 
iTemp = iTemp(iNeq); 

% take the sign of the first sample derivative 
s = sign(diff(yTemp(iTemp))); 

% find local maxima 
iMax = [false;diff(s)<0]; 
iPk = iTemp(iMax)-1; 

pksAndFollowingIdx = [iPk.';iPk.'+1]; %get neighbouring +ve and -ve values 
deltaOfTime = diff(origVec(pksAndFollowingIdx))./iPk.'; %take difference between consecutive positive and negative values 

也就是說,如果你原來的代碼應該是更多的東西一樣:

for i = 1:10-1 % loop through array??? 
    if (origVec(i)>0) && (origVec(i+1) <0) % check neighbouring +ve THEN -ve values??? 
     tan12 = ((origVec(i+1))- origVec(i)); %calculate difference??? 
     deltaOfTime(i) = tan12/i % deltaOfTime, not sure how this is "delta of time"??? 
    end 
end 
0

您應該保存每次計算而不是覆蓋每個循環的價值:

deltaOfTime = zeros(1,1000000); 
for i = 1:1000000 % loops strats from 1 and ends at 1000000 
    if (chA(i)<0) && (chA((i+1) >0)) % if i = 1, i+1 = -3 <it satisfy the condition> 
    tan = ((chA(i+1))- chA(i)); %calculate it 
    deltaOfTime(i) = tan/i; %store the value here in the vector deltaOfTime 
end 

但有更好的方法來計算的過渡,你會不會通過你的信號需要循環,或預分配大型矢量deltaOfTime

這是一種方法,不預先分配值,但是它可能是因爲內環路陣列變化較慢:

for i = 1:1000000 % loops strats from 1 and ends at 1000000 
    if (chA(i)<0) && (chA((i+1) >0)) % if i = 1, i+1 = -3 <it satisfy the condition> 
    tan = ((chA(i+1))- chA(i)); %calculate it 
    deltaOfTime = cat(2,deltaOfTime,tan/i); %store the value here in the vector deltaOfTime 
end 

另一種嘗試糾正代碼中的所有錯誤:

for i = 1:length(chA)-1 
    if (chA(i)<0) && (chA((i+1) >0)) 
     temp = ((chA(i+1))- chA(i)); 
     deltaOfTime = cat(2,deltaOfTime,temp/i); 
    end 
end 

修復了if語句,以及一個循環條件,如果您的數組長度恰好爲1百萬,那麼將會給您一個錯誤。


注:避免使用現有函數的變量名,例如, tan。注意2:您確定不希望tandeltaOfTime的定義位於if-陳述內嗎?

+0

感謝這就是看上去preety有幫助。但我不想存儲將作爲零休息的價值,我將存儲。可能嗎? –

+0

謝謝你看起來很有幫助。但我不想存儲將作爲零或零休息我將存儲的值。因爲我需要在圖上繪製deltaOfTime的結果。可能嗎? –

+0

將數組視爲[0,1,-3,4,5,6,7,8,9,-19] (對於i = 1:1000000%從1開始循環策略並結束於1000000 if(chA(i如果i = 1,則i + 1 = -3 <滿足條件> tan =((chA(i + 1)) - chA(一世)); %計算它 deltaOfTime = tan/i; %在這裏將值存儲在向量deltaOfTime中 end 現在在下一次迭代中,它應該能夠找出下一個連續的正值和負值,即9,-19 –