# Trying to work out transpose of n sized matrix

This topic is 4518 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[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,

1. 1
Rutin
24
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002142
×