Trying to work out transpose of n sized matrix

Started by
1 comment, last by johnnyBravo 18 years, 1 month ago
Hi, I don't know what is wrong with me, but I can't work out how to get the transpose of a n sized matrix. I can do it fine manually, but I can't wrap my head around it. So if I had the code:

const int n = 4;
int matrix[n][n];
//initialize matrix...


can someone tell me an algorithm to transpose this matrix? Thanks
Advertisement
Just remember that m[j] and m[j] are swapped, and diagonal elements are unchanged. There are various ways to implement the function, depending I suppose on how concerned you are with efficiency. Here's one way you could do it (no guarantee of correctness):
for (int j = 1; j < size; ++j) {    for (int i = 0; i < j; ++i) {        float temp = m[j];        m[j] = m[j];        m[j] = temp;    }}
Ah right thanks,

This topic is closed to new replies.

Advertisement