2017-10-28 420 views
0

寫出函數tokenizeSignal(signal),它取上面的信號並計算出0和1的順序出現的次數。輸出應該是一個二維數組,其中列1是多少出現,列2是哪個令牌(0或1)。我有下面的代碼,直到我把它放入函數。例如MATLAB摩爾斯電碼

sig =[1 1 0 0 0 0 0 1 1 0 1 0 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 0]; 
tsig = abs(sig); 
dsig = diff([1 tsig 1]); 
startIndex = find(dsig<0); 
endIndex = find(dsig>0)-1; 
duration = endIndex-startIndex+1; 
stringIndex = (duration >= 2); 
d=find(stringIndex==0); 
matA=[duration;zeros(1,size(duration,2))]; 
matA=matA'; 
wsig = abs(sig); 
rsig = diff([0 wsig 0]); 
startIndex = find(rsig < 0); 
endIndex = find(rsig > 0)-1; 
duration = endIndex-startIndex+1; 
abs(duration); 
stringIndex = (duration >= 2); 
d=find(stringIndex==0); 
type=[1]; 
matB=[ans;ones(1,size(ans,2))]; 
matB=matB'; 
token=reshape([matA(:) matB(:)]',size(matA,1)+size(matB,1), []) 

這將返回我們所需要的,但是當我們把上面的代碼放到一個函數頭和類型結束的結論不再返回任何東西。爲什麼是這樣?

回答

0

它不工作的原因是因爲你靠關鍵字 「答」,這是從工作區訪問,而不是一個函數中,指的是ABS(持續時間)

此複製你的腳本,裏面功能:

function tokens = tokenizeSignal(sig) 
tsig = abs(sig); 
dsig = diff([1 tsig 1]); 
startIndex = find(dsig<0); 
endIndex = find(dsig>0)-1; 
duration = endIndex-startIndex+1; 
matA=[duration;zeros(1,size(duration,2))]; 
matA=matA'; 
wsig = abs(sig); 
rsig = diff([0 wsig 0]); 
startIndex = find(rsig < 0); 
endIndex = find(rsig > 0)-1; 
duration = endIndex-startIndex+1; 
yourAns = abs(duration); 
matB=[yourAns;ones(1,size(yourAns,2))]; 
matB=matB'; 
tokens=reshape([matA(:) matB(:)]',size(matA,1)+size(matB,1), []) ; 
+0

謝謝,這是非常有幫助的。現在我得到一個錯誤,因爲在一些測試條件下,matA和matB是不同的大小。我應該如何重塑它們才能獲得令牌? –

+0

您可以返回一個單元格數組,如 –

+0

tokens = {matA(:); MATB(:)}; 用這個替換函數的最後一行。 HTH,PSS –