Hey, I might be having a blonde moment but here goes..

I'm trying to split a massive 1D vector into blocks at different places (If that makes sense), it should basically go like this:

0
201
401
601
801
..
..
..,
57201
2
202
402
602
..
..
..
..
3
302
402
602

But, for some reason, it prints out as:
0
1
2
3
..
..
..

Here's a tiny bit of sample code:

for(unsigned i=0; (i < numblocks); i++)
{
   for(unsigned j=0; (j < N); i++)
   {
       cout << (i*200+j) << endl; 
   }
 }

In my head (and on paper) it should go.. 0, 201, 202.. But it doesn't.. Any ideas? Thanks :)

Recommended Answers

All 3 Replies

In your inner loop you are incrementing i instead of j. I cant tell if this is a copy-paste error or not but it would lead to a result other than what you want and what you are getting.
Please paste the actual code you are using so that we may evaluate.

Hello,

So basically, here is my code:

vector<iniMatrix> Audio::subBlocks(vector<float>& theData, int N, int M)
{

    int n = theData.size();
    int maxblockstart = n - N;
    int lastblockstart = maxblockstart - (maxblockstart % M);

    int numblocks = (lastblockstart)/M + 1;

    vector<float> subBlock;
    vector<iniMatrix> block(numblocks);

    for(int i=0; (i < numblocks); i++)
    {
        for(int j=0; (j < N); j++)
        {
            subBlock.push_back(theData[i*M+j]);

            block[i].push_back(subBlock[i]);

        } 
    }

    cout << block[0][0];
    return block;
}

I'm converting the code from Matlab and the results don't match.. For example:

If I output block[1][2] it will give me 203 however, block[1][2] in matlab is 200..

Given your description, block[1][2] should output 202 (1 * 200 + 2).
In your sample from matlab, if the rows are your i values and the columns your j values then you have a match (202 at that location). You have to realize that the table is 1-based and vectors use zero-based indexing.
I'd probably change the code to something like the following as I find it more readable:

vector<iniMatrix> block(numblocks);
for(int i=0; i < numblocks; i++) {
    vector<float> subBlock;
    for(int j=0; j < N; j++)
        subBlock.push_back(theData[i*M+j]);
    block[i].push_back(subBlock);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.