1,105,229 Community Members

Transpose a matrix

Member Avatar
mariners95
Newbie Poster
2 posts since Feb 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I need help with transposing a matrix.

I already did the transpose matrix using 2 arrays.

But I can't figure out how to do it using only one array.
So read a matrix and create the transpose within itself.
Do I use the swap or what?

Any help would be great.

Later

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

If it's a square matrix then you can do it with something like this:

for (int i = 0; i < 4; i++) {
  for (int j = i + 1; j < 4; j++) {
    int save = matrix[i][j];
    matrix[i][j] = matrix[j][i];
    matrix[j][i] = save;
  }
}

If it's a non-square matrix then you're SOL.

Member Avatar
murschech
Junior Poster in Training
60 posts since Dec 2004
Reputation Points: 11 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

If it's a square matrix then you can do it with something like this:

for (int i = 0; i < 4; i++) {
  for (int j = i + 1; j < 4; j++) {
    int save = matrix[i][j];
    matrix[i][j] = matrix[j][i];
    matrix[j][i] = save;
  }
}

If it's a non-square matrix then you're SOL.

Your program won't work. The reason is that when you write matrix[j] = matrix[j] you've lost the contents of matrix[j] forever and you can't get it back to put in position [[j]. This will do it.

for (int i = 0; i < 4; i++)
   for (int j = 0; j < 4; j++)
    {   save = matrix[i][j];
        matrix[i][j] = matrix[j][i];
         matrix[j][i] = save;
    }
Member Avatar
murschech
Junior Poster in Training
60 posts since Dec 2004
Reputation Points: 11 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

If it's a square matrix then you can do it with something like this:

for (int i = 0; i < 4; i++) {
  for (int j = i + 1; j < 4; j++) {
    int save = matrix[i][j];
    matrix[i][j] = matrix[j][i];
    matrix[j][i] = save;
  }
}

If it's a non-square matrix then you're SOL.

Sorry. I didn't read your code carfully enough before I replied.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

>Sorry. I didn't read your code carfully enough before I replied.
Maybe you should run example code to see if it works before trying to correct it with an incorrect solution. Starting the inner loop at 0 instead of i + 1 will result in a lot of shuffling to get the same matrix that you started with, not a transposed matrix as the OP requested. So maybe you should also test your own code before posting it. Especially if you're trying to correct someone and using it as an example.

On a purely stylistic point, I have two issues with your code. First, even though it's valid the way you did it, you should always put braces around a loop or if construct that has more than a one line body. This way you don't have to rely solely on indention to prove that your code is correct. You also avoid certain pitfalls.

Second, starting the statements of a block on the same line as the opening brace is a formatting nightmare. It makes code harder to read and harder to reformat so that it's easier to follow.

Something more like this (following your apparent brace indention style):

for (int i = 0; i < 4; i++)
 {
   for (int j = 0; j < 4; j++)
    {
       save = matrix[i][j];
       matrix[i][j] = matrix[j][i];
       matrix[j][i] = save;
    }
 }

You also failed to define save before using it, but I'll let you slide on that. ;)

Member Avatar
aldrinsms
Newbie Poster
3 posts since Sep 2007
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

How to make a program yhat make the transpose of a matrix using array and pointers C ofcourse?

Member Avatar
Dave Sinkula
long time no c
4,852 posts since Apr 2004
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
Team Colleague
 
0
 

Wonderful! Be a pompous ### and dredge up an old thread (after reiterating the same demand a 3rd time), offering nothing, and pretend you are being courteous! Well, done. *plonk*

Member Avatar
MANDALSHASHI
Newbie Poster
3 posts since Nov 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

// prog to mulltiply matrix

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,m1,n1;
clrscr();
printf("Enter array size ");
scanf("%d%d",&m,&n);
printf("Enter array size 2");
scanf("%d%d",&m1,&n1);
if(n!=m1)
{
printf("Wrong choice entered");
getch();
exit(0);
}
else
{
printf("Enter elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter element 2");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("REQUIRED MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<n1;j++)
{
printf("\n%d",c[i][j]);
}
printf("\n");
}
getch();
}
}
Member Avatar
thinkammar
Newbie Poster
1 post since Mar 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi there
I read you method of transposing a matrix
it works
but i have toa sk a question!
that what is the logic behind initializing inner loop variable j=i+1 instead of j=0???
i shalll be thankful to you if you reply to this post
thanx

Member Avatar
Ancient Dragon
Achieved Level 70
27,572 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

>>what is the logic behind initializing inner loop variable j=i+1 instead of j=0???

which post # are you talking about?

Member Avatar
nucleon
Posting Pro in Training
476 posts since Oct 2008
Reputation Points: 114 [?]
Q&As Helped to Solve: 92 [?]
Skill Endorsements: 0 [?]
 
0
 

You only need to loop through less-than-half of the matrix since in each execution of the inner loop you're swapping two elements (and the main diagonal stays put). If you iterated through the whole matrix, you'd just put it back the way it started. Starting j at i+1 loops through the upper triangle of the square (or lower, depending on how you look at it), swapping elements with the opposite triangle.

Member Avatar
marvind786
Newbie Poster
1 post since Sep 2009
Reputation Points: -8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-2
 

i want to find determinant of a matrix in java where the order of the matrix is taken at run time.. i'm implementing hill cipher in java.. please help me out..

Member Avatar
Nit90esh
Newbie Poster
1 post since Oct 2011
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 
/*this is easy transpose of matrix*/
void(int mat[],int m,int n)
 {
  int t[10][10],i,j;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      t[i][j]=mat[j][i]
     }
   }
 }
srivanip
Newbie Poster
1 post since May 2012
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
-1
 

The above code needs a small change. If you use the above code for transpose, you will end up getting the same matrix as before because, you are replacing rows and columns twice.

You need to do the replacement only once.

for ( i=0; i<3; i++)
    { 
        for ( j=0; j<3; j++)
        {
           if(i>j)
            {
                temp = inputmatrix[i][j];
                inputmatrix[i][j] = inputmatrix[j][i];
                inputmatrix[j][i] = temp;
            }
        }
    };
johnnew
Newbie Poster
1 post since Dec 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
-2
 

if anyone is interested in watching a video tutorial for transposing a matrix:
Click Here

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article