Transpose a matrix

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2005
Posts: 2
Reputation: mariners95 is an unknown quantity at this point 
Solved Threads: 0
mariners95 mariners95 is offline Offline
Newbie Poster

Transpose a matrix

 
0
  #1
Feb 13th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Transpose a matrix

 
0
  #2
Feb 13th, 2005
If it's a square matrix then you can do it with something like this:
  1. for (int i = 0; i < 4; i++) {
  2. for (int j = i + 1; j < 4; j++) {
  3. int save = matrix[i][j];
  4. matrix[i][j] = matrix[j][i];
  5. matrix[j][i] = save;
  6. }
  7. }
If it's a non-square matrix then you're SOL.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: Transpose a matrix

 
0
  #3
Feb 15th, 2005
Originally Posted by Narue
If it's a square matrix then you can do it with something like this:
  1. for (int i = 0; i < 4; i++) {
  2. for (int j = i + 1; j < 4; j++) {
  3. int save = matrix[i][j];
  4. matrix[i][j] = matrix[j][i];
  5. matrix[j][i] = save;
  6. }
  7. }
If it's a non-square matrix then you're SOL.
Your program won't work. The reason is that when you write matrix[i][j] = matrix[j][i] you've lost the contents of matrix[i][j] forever and you can't get it back to put in position [[j][i]. This will do it.
  1. for (int i = 0; i < 4; i++)
  2. for (int j = 0; j < 4; j++)
  3. { save = matrix[i][j];
  4. matrix[i][j] = matrix[j][i];
  5. matrix[j][i] = save;
  6. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: Transpose a matrix

 
0
  #4
Feb 15th, 2005
Originally Posted by Narue
If it's a square matrix then you can do it with something like this:
  1. for (int i = 0; i < 4; i++) {
  2. for (int j = i + 1; j < 4; j++) {
  3. int save = matrix[i][j];
  4. matrix[i][j] = matrix[j][i];
  5. matrix[j][i] = save;
  6. }
  7. }
If it's a non-square matrix then you're SOL.
Sorry. I didn't read your code carfully enough before I replied.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Transpose a matrix

 
0
  #5
Feb 15th, 2005
>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):
  1. for (int i = 0; i < 4; i++)
  2. {
  3. for (int j = 0; j < 4; j++)
  4. {
  5. save = matrix[i][j];
  6. matrix[i][j] = matrix[j][i];
  7. matrix[j][i] = save;
  8. }
  9. }
You also failed to define save before using it, but I'll let you slide on that.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 3
Reputation: aldrinsms is an unknown quantity at this point 
Solved Threads: 0
aldrinsms aldrinsms is offline Offline
Newbie Poster

Re: Transpose a matrix

 
-1
  #6
Sep 30th, 2007
How to make a program yhat make the transpose of a matrix using array and pointers C ofcourse?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Transpose a matrix

 
0
  #7
Sep 30th, 2007
Wonderful! Be a pompous ass 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*
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 3
Reputation: MANDALSHASHI is an unknown quantity at this point 
Solved Threads: 0
MANDALSHASHI's Avatar
MANDALSHASHI MANDALSHASHI is offline Offline
Newbie Poster

Re: Transpose a matrix

 
0
  #8
Nov 23rd, 2008
// prog to mulltiply matrix
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a[10][10],b[10][10],c[10][10],i,j,k,m,n,m1,n1;
  6. clrscr();
  7. printf("Enter array size ");
  8. scanf("%d%d",&m,&n);
  9. printf("Enter array size 2");
  10. scanf("%d%d",&m1,&n1);
  11. if(n!=m1)
  12. {
  13. printf("Wrong choice entered");
  14. getch();
  15. exit(0);
  16. }
  17. else
  18. {
  19. printf("Enter elements");
  20. for(i=0;i<m;i++)
  21. {
  22. for(j=0;j<n;j++)
  23. {
  24. scanf("%d",&a[i][j]);
  25. }
  26. }
  27. printf("Enter element 2");
  28. for(i=0;i<m1;i++)
  29. {
  30. for(j=0;j<n1;j++)
  31. {
  32. scanf("%d",&b[i][j]);
  33. }
  34. }
  35. for(i=0;i<m;i++)
  36. {
  37. for(j=0;j<n1;j++)
  38. {
  39. c[i][j]=0;
  40. for(k=0;k<n;k++)
  41. {
  42. c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
  43. }
  44. }
  45. }
  46. printf("REQUIRED MATRIX");
  47. for(i=0;i<m;i++)
  48. {
  49. for(j=0;j<n1;j++)
  50. {
  51. printf("\n%d",c[i][j]);
  52. }
  53. printf("\n");
  54. }
  55. getch();
  56. }
  57. }
Last edited by Ancient Dragon; Mar 23rd, 2009 at 3:53 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 1
Reputation: thinkammar is an unknown quantity at this point 
Solved Threads: 0
thinkammar thinkammar is offline Offline
Newbie Poster

Re: Transpose a matrix

 
0
  #9
Mar 23rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,678
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Transpose a matrix

 
0
  #10
Mar 23rd, 2009
>>what is the logic behind initializing inner loop variable j=i+1 instead of j=0???

which post # are you talking about?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 26544 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC