基本上,我试图将一个巨大的 1D 向量拆分为通过函数传递的给定大小的块。该函数应该返回一个二维向量,然后我可以访问不同的块。我找到了一个合适的算法来做到这一点,但是,它在 Matlab 中,我不明白如何将元素放在 2D 向量中。
MatLab 代码:
function f = block(v, N, M)
n = length(v);
maxblockstart = n - N + 1;
lastblockstart = maxblockstart - mod(maxblockstart-1 , M);
numblocks = (lastblockstart-1)/M + 1;
f = zeros(numblocks,N);
for i = 1:numblocks
for j = 1:N
f(i,j) = v((i-1)*M+j);
end
end
这是我在 C++ 中的尝试(如果不好,请见谅):
vector<iniMatrix> subBlocks(vector<int>& theData, int N, int M)
{
// This method splits the vector into blocks
// Each block has size N.
// and consecutive blocks differ
int n = theData.size();
int maxblockstart = n - N+1;
int lastblockstart = maxblockstart - (maxblockstart-1 % M);
int numblocks = (lastblockstart-1)/M + 1;
vector<int> subBlock;
vector<iniMatrix> block;
for(unsigned i=0; (i < numblocks); i++)
{
for(unsigned j=0; (j < N); j++)
{
subBlock.push_back(theData[(i-1*M+j)]);
block.push_back(subBlock);
}
}
return block;
}
这段代码可以编译,但是,当我试图输出块的大小时,我得到:分段错误:11 ..有什么想法吗?
通过函数传递的数据是:N = 600 M = 200
我希望有人可以帮助我,谢谢:)