Error in inner product of vectors and index

In summary, Jedishrfu is having difficulty debugging his code. He tried printing the variables step-by-step but could not find his error. He then tried to calculate the inner product of each row from matrix B with a corresponding row from matrix A, but the code did not work correctly. He eventually figured out that he forgot to use all the elements of matrix B and fixed the code.f
  • #1
Hello

I found a bug in my code and can't figuring out the error. I tried debugging by showing the output of each variable step by step but I can't find my error. Here is what I have and what I want to do:

I have a matrix A:
0000
0101
1010
1111

And I have a matrix B:
10000
21000
30100
41100
20010
21010
40110
41110
30001
41001
30101
41101
40011
41011
40111
41111

The matrix B has 16 rows and 5 coloumns. The matrix A has 4 rows and 4 coloumns. Now I declare a matrix C that has 4 rows and 16 coloumns.

What I want to do is to calculate the inner product of each row from B with a corresponding row from A. With corresponding I mean that the first coloumn of B shoud define the row from A that I want to multiply. So the B matrix has in fact also four-dimensional vectors and the first element corresponds to the row of A. One could say this first coloumn of B is an index for choosing the row of A. Because C++ start counting by zero I substract one for my index. Here is my code:


Code:
std::vector< std::vector<int> > C(4, std::vector<int>(16));
std::vector<int> index(4);
std::vector<int> vectorA(4);
std::vector<int> vectorB(4);

for( int y = 0; y < 16; y++)
{
     
    for(int i=0; i<4; ++i){
    vectorA[i] = A[ B[y][0]-1 ][i];
    }

    for( int x = 1; x < 4; x++)
    {

        vectorB[x -1] = B[y][x];

    }


C[ B[y][0] -1][index[ B[y][0] -1] ] = inner_product(vectorA.begin(), vectorA.end(), vectorB.begin(), 0);

    index[B[y][0]-1] += 1;
}


This results in my matrix C:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 3 1 2 1 2 2 3 0 0 0 0 0 0 0

The first two rows are correct but row three and four are false.
The correct solution has to be (maybe except of ordering in row 3 and 4):

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
4 3 3 2 3 2 3 2 2 0 0 0 0 0 0 0

Where is my problem? Please help, it drives me crazy :(

Thanks and greetings.
 
  • #2
Why not place print statements inside the for loops and let the computer show you what it is doing at each step or better yet if you have a debugger step through your code and compare your understanding of the process with what the computer is doing?
 
  • #3
Hi

I did exactly this but can't figure it out. I guess there must be a problem in my coding logic?
 
  • #4
What I'm saying is you need to vet each value the computer prints out and agree that its what you expect to see. Only then will you either find your error or realize that maybe your understanding of the algorithm is wrong.
 
  • #5
I have to learn a lot and will spend more time with this. I understand what you mean and tried it. Can you give me a hint what could be wrong in my code? For someone who knows C++ this should be an easy beginners question?
 
  • #6
Not understanding your code but the loop where you set x=1 shouldn't it use x<5?
 
  • #7
For someone who knows C++ this should be an easy beginners question?

It might be an easy question if you give a complete program that can be compiled. It's hard to debug code fragments.
 
  • #8
@jedishrfu

You are totally right. With x<5 the program works correct. Now it makes sense. But I have to say I am a little sad that I can't figure this out by printing the variables step by step :( I think I will write little programs where I know exactly what's happening and make explicit error in the loops for learning and studying debugging. Thanks for help
 
  • #9
Yes, that's an excellent way to learn a language usually though it will be compile mistakes that occur.

However, there are some logic errors like this that can be exasperating but you collect these gems in your head and vow not to make the same error again.

In your problem here, I would have printed the loop values and the other values so that I could step thru each operation and then it might have occurred to me that wait I didn't use all the elements of B.

Also I'd suggest learning to use a debugger that way you an skip the print statements and focus on your code. This also eliminates the potential problem of a bug in your print statements.
 

Suggested for: Error in inner product of vectors and index

Replies
8
Views
1K
Replies
1
Views
765
Replies
13
Views
698
Replies
10
Views
952
Replies
6
Views
1K
Replies
23
Views
3K
Replies
3
Views
985
Replies
7
Views
1K
Back
Top