Welcome Guest ( Log In | Register ) | Resend Validation Email |
DigiBunny | Posted: Aug 7 2011, 07:34 AM | ||
Reading is magic! Group: Advanced Members Posts: 2,601 Member No.: 30,700 Joined: Sep 24th 2008 Location: Philippines | Right. So I'm trying to multiply one matrix by another matrix, then display the output using for() loops. Spoilered for length and ease of access.
I've had more than a few brain freeze-ups because I couldn't follow the numbers. Even when I tried to trace it on paper, I kept losing track of what would happen if I modified one of the loop counters. I'm not expecting an instant solution, but if someone sees any logical errors with the code, please point them out. | ||
Perry | Posted: Aug 7 2011, 12:27 PM |
Like an eagle! Group: SITE OWNER Posts: 8,014 Member No.: 1 Joined: Sep 15th 2002 Location: San Leandro, California | What you want to do is try something simpler, like 2x3 or 2x2 matrices. Then do all the iterations on paper. You must know exactly what happen in each for loop before you can continue.. |
Proud Contributor of the Music Section Revival Project |
Kiroshino | Posted: Aug 7 2011, 04:25 PM |
IDW Goldmember Group: FORUM MODERATOR Posts: 2,453 Member No.: 34,203 Joined: Oct 22nd 2009 Location: NJ, USA | Haven't touched programming in a while, but from what I see, Compiler is reset to zero with every iteration of the inner-most for loop. The entry in FinalMatrix is given the new value of Compiler with every iteration of the inner-most for loop. Hence the reason why most of your FinalMatrix entries are zero. Didn't double check, but I think this should work: for(int i = 0; i <5; i++) { for(int j = 0; j < 4; j++) { float Compiler = 0; for(int k = 0; k < 4; k++) { Compiler += P[i][k] * Rx[k][i]; } FinalMatrix[i][j] = Compiler; } } This post has been edited by Kiroshino on Aug 7 2011, 04:33 PM |
DigiBunny | Posted: Aug 7 2011, 06:11 PM |
Reading is magic! Group: Advanced Members Posts: 2,601 Member No.: 30,700 Joined: Sep 24th 2008 Location: Philippines | The simpler matrix was a grand idea Perry. That let me scale down the problem. Kiro; I did something similar just now before I saw your code. We're essentially identical except for where we reset Compiler's value. Mine is at the end of the j loop instead of at the beginning; but the answer remains the same. On the bright side, I've isolated the problem. I used a new matrix set; M = {{1,0},{0,1}} As an Identity Matrix N = {{2,3},{5,4}} As my example. At this point, I should only get the value for N back since multiplying by M gives me the same thing. Thus, eliminating the variable of error in human calculations. [Y x] [x x] [x x] and [x Y] are the only ones being hit correctly. Everything else takes on the same values for that row. In other words, I'm missing the top right and bottom left parts of the resulting matrix, probably because I need another loop to stay on one row, while evaluating all the columns on that row, and vice versa. |
Kiroshino | Posted: Aug 7 2011, 07:27 PM | ||
IDW Goldmember Group: FORUM MODERATOR Posts: 2,453 Member No.: 34,203 Joined: Oct 22nd 2009 Location: NJ, USA | Don't have something to test it with, but I think this is the cause: for(int i = 0; i <5; i++) { for(int j = 0; j < 4; j++) { float Compiler = 0; for(int k = 0; k < 4; k++) { //Compiler += P[i][k] * Rx[k][i]; Compiler += P[i][k] * Rx[k][j]; } FinalMatrix[i][j] = Compiler; } } Note the change from i to j. Slightly easier to follow version:
This post has been edited by Kiroshino on Aug 7 2011, 07:32 PM | ||
DigiBunny | Posted: Aug 7 2011, 09:42 PM | ||
Reading is magic! Group: Advanced Members Posts: 2,601 Member No.: 30,700 Joined: Sep 24th 2008 Location: Philippines | Done! It's refreshing to finally get this thing right. I've included the source code below, should anyone, for any remote reason, find that they need this. Development notes are included. Now all I need to do is make this work on a much larger scale. Since the logic is the same, I'm under the impression it should be as simple as replacing the Matrices, then changing the maximum values of the for() loops to suit them.
Though, I'm a little puzzled as to how I'll multiply matrixes more than once every time I run the code. | ||
Kiroshino | Posted: Aug 8 2011, 05:15 AM |
IDW Goldmember Group: FORUM MODERATOR Posts: 2,453 Member No.: 34,203 Joined: Oct 22nd 2009 Location: NJ, USA | What do you mean? Matrix1 x Matrix2 x MatrixN? |