# Trying to work out transpose of n sized matrix

This topic is 4301 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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;    }}

Ah right thanks,

##### Share on other sites

This topic is 4301 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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

• ### Forum Statistics

• Total Topics
628699
• Total Posts
2984277

• 20
• 10
• 13
• 13
• 11