转换 MatLab 代码 - 困惑
Converting MatLab code - Confused
基本上,我有最后一段代码可以从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)
有什么想法吗?谢谢:)
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)。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 大学里令人困惑和过于复杂的代码示例
- 对这行代码感到困惑,该代码行旨在充当将类引入到空指针中
- 我在区分源代码、目标代码、汇编代码和机器代码时感到困惑
- 在代码块中的SDL_SHOWSIMPLEMESSAGEBOX中,令人困惑的错误,不确定的引用和未定义的引用
- 转换 MatLab 代码 - 困惑
- 对以下代码感到困惑
- 我可能会对std::set工作感到困惑。我的代码无法正常工作
- 如何调试令人困惑的大代码
- 这段代码是否在作为参数传递时处理数据有点困惑
- 一些反汇编c++代码我很困惑
- 对c++结构声明中代码的目的/意义感到困惑
- 对我的代码如何工作感到困惑,并想了解其中的逻辑
- 在基于文本的冒险游戏中,我该如何避免冗长而令人困惑的条件代码
- 用于复制文件的c++代码::对相对地址感到困惑(波浪)