转换MatLab代码 - 困惑

时间:2012-08-16 16:41:58

标签: c++ matlab

基本上,我有最后一段代码可以从MatLab转换为C ++。

该函数接收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

或者,不是返回另一个向量(无论是1D还是2D),实际发送向量的内存位置并从中删除块可能更好吗?所以例如..

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 个答案:

答案 0 :(得分: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,要么是(最大)X(len)。