gpt4 book ai didi

c++ - 转换 MatLab 代码 - 困惑

转载 作者:行者123 更新时间:2023-11-28 03:32:53 24 4
gpt4 key购买 nike

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

该函数接受一个 2D vector ,然后根据 2 个标准检查 2D vector 的元素,如果不匹配,它会删除 block 。但是我对 MatLab 中的代码想要返回什么感到困惑,是二维 vector 还是一维 vector ?这是代码:

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

或者,与其返回另一个 vector (无论是一维还是二维),不如实际发送 vector 的内存位置并从中删除 block 会更好吗?例如..

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 到结尾]。所以如果它是一个 3x3 数组,它会做类似的事情:

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

然后它取矩阵内容的绝对值并将它们相加。它返回一个 [x][x] 矩阵,但长度要么是 0x0 要么是 (max)X(len)。

关于c++ - 转换 MatLab 代码 - 困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11991814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com