Can't multiply matrices

Hey,
I am having some trouble multipliyng matrices, here is my code.

for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
AnT[i][j]=An[j][i];
}
}

for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
for(int k=0;k<n;k++) {
H1[i][j]+=AnT[i][k]*A[k][j];
}
}
}

I have no red lines in my program, and Ant is the tranposed An.
When I type it out I get 4x4 matrix, same dimension as AnT and An, but only H1[0][0],H1[0][1] and H1[1][0] have the correct coordinates. Is my matrix multipliyer wrong?
Thanks in advance and best regards,
First time poster so sorry if I posted something not according to customs.
Last edited on
Difficult to tell from this fragment of code alone. You do need to set H1[i][j]=0 before the k loop; i.e. before you start adding to an unknown quantity.

If this is the only place that you use the transpose matrix AnT then you could rephrase the single line doing much in the matrix multiply to avoid having it at all.
If there is a really, really bad condition factor you could get a "wrong" answer from roundoff. Seems unlikely on a 4x4, it would have to be really bad.

you didnt do anything major, but for significant code blocks use the formatting <> for it.
The standard posting format eats all the white space and is a non fixed width font, both of which mess up code presentation.

have you tested the transpose?

the reason to transpose is for large matrix, you can iterate rows on both, instead of a row and a col, the cols create page faults and cache misses on larger data.
Last edited on
Topic archived. No new replies allowed.