2012-08-16 120 views
0

基本上,我有這最後一段代碼轉換從MATLAB到C++。轉換MATLAB代碼 - 困惑

該函數採用在2D矢量,然後檢查針對2個標準2D向量的元素,並且如果不匹配,則刪除的塊。但是我對MatLab中的代碼想要返回的是2D還是1D矢量感到困惑?下面是代碼:

function f = strip(blocks, sumthresh, zerocrossthresh) 

% This function removes leading and trailing blocks that do 
% not contain sufficient energy or frequency to warrent consideration. 
% Total energy is measured by summing the entire vector. 
% Frequency is measured by counting the number of times 0 is crossed. 
% The parameters sumthresh and zerocrossthrech are the thresholds, 
% averaged across each sample, above which consideration is warrented. 

% A good sumthresh would be 0.035 
% A good zerocrossthresh would be 0.060 

len = length(blocks); 
n = sum(size(blocks)) - len; 
min = n+1; 
max = 0; 
sumthreshtotal = len * sumthresh; 
zerocrossthreshtotal = len * zerocrossthresh; 
for i = 1:n 
currsum = sum(abs(blocks(i,1:len))); 
currzerocross = zerocross(blocks(i,1:len)); 
if or((currsum > sumthreshtotal),(currzerocross > zerocrossthreshtotal)) 
if i < min 
    min = i; 
end 
if i > max; 
    max = i; 
    end 
end 
end 

% Uncomment these lines to see the min and max selected 
% max 
% min 

if max > min 
    f = blocks(min:max,1:len); 
else 
f = zeros(0,0); 
end 

或者,而不是返回另一個向量(無論是一維或二維)可能是更好實際發送向量的存儲位置,並從中取出塊?因此,例如..

for(unsigned i=0; (i < theBlocks.size()); i++) 
{ 
    for(unsigned j=0; (j < theBlocks[i].size()); j++) 
    { 
     // handle theBlocks[i][kj] .... 
    } 
} 

另外,我不明白這行:

currsum = sum(abs(blocks(i,1:len))); 

基本上:(I,1:LEN)

任何想法?謝謝:)

+1

看着你所有的問題最後我得到的所以感覺的用戶在做大部分工作對您的MATLAB到C++的轉換。 – 2012-08-16 17:11:00

+0

我不是要求代碼,我是要求人們的意見。 – Phorce 2012-08-16 17:25:25

+1

你想知道回報是什麼?最後,如果語句顯而易見,如果您仍然不明白,爲什麼不直接執行代碼。你想知道總和(abs(blocks()))在做什麼?你爲什麼不嘗試求助求和,幫助abs並檢查塊(x,y)中存儲的內容。這不是要問其他人他們認爲這只是懶惰而已。 – 2012-08-16 17:37:59

回答

2

blocks(i,1:len)告訴陣列,它想從blocks[i][1 to the end]去。所以,如果它是一個3x3的陣列它做喜歡的事:

blocks[i][1] 
blocks[i][2] 
blocks[i][3] 
. 
. 
. 
blocks[i][end] 

然後它採取的矩陣內容的絕對值相加,然後。它返回一個[x] [x]矩陣,但長度要麼是0x0,要麼是(max)X(len)。