Sign in to follow this  
johnnyBravo

Trying to work out transpose of n sized matrix

Recommended Posts

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

Share this post


Link to post
Share on other sites
Just remember that m[i][j] and m[j][i] 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[i][j];
m[i][j] = m[j][i];
m[j][i] = temp;
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this