How to transpose a matrix?

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

Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

How to transpose a matrix?

 
1
  #1
Jan 6th, 2006
Hello everyone,


I am wondering how to transpose a matrix (m * n) with only constant space complexity O (1). (The transpose algorithm should execute/operate on the original matrix.)


thanks in advance,
George
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: How to transpose a matrix?

 
0
  #2
Jan 6th, 2006
Unless the matrix is square you won't be able to handle that, as arrays are immutable in size.
  1. | 1 2 3 |
  2. | 4 5 6 |
  3. == [[1,2,3],[4,5,6]]
Can't be put in the same array as
  1. | 1 4 |
  2. | 2 5 |
  3. | 3 6 |
  4. == [[1,4],[2,5],[3,6]]

You're going to need some temporary storage anyway if you could get away with it (which you can with square matrices).
dest[i,j] = org[j,i] For each i,j;
Therefore you're going to have to store the value you're replacing until you've a place to put it (which is easy as you can quite readily figure out as you're effectively swapping valued in that scenario).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: How to transpose a matrix?

 
0
  #3
Jan 7th, 2006
jwenting,


Originally Posted by jwenting
Unless the matrix is square you won't be able to handle that, as arrays are immutable in size.
  1. | 1 2 3 |
  2. | 4 5 6 |
  3. == [[1,2,3],[4,5,6]]
Can't be put in the same array as
  1. | 1 4 |
  2. | 2 5 |
  3. | 3 6 |
  4. == [[1,4],[2,5],[3,6]]

You're going to need some temporary storage anyway if you could get away with it (which you can with square matrices).
dest[i,j] = org[j,i] For each i,j;
Therefore you're going to have to store the value you're replacing until you've a place to put it (which is easy as you can quite readily figure out as you're effectively swapping valued in that scenario).
I think it is possible to represent a matrix by a linear array -- in that way, I think we can handle that. Do you agree?

Anyway, if you agree with that, do you have any ideas?


regards,
George
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How to transpose a matrix?

 
0
  #4
Jan 7th, 2006
I think it is possible to represent a matrix by a linear array
If by linear u mean a 1 dimensional array, then yes thats possible. But to do so would B just stupid...

By convention matrices should be defined using two dimensions, row and column, this actually makes it easier to manipulate matrices :rolleyes:

And there are literally hundreds examples of matrix demos out there. Just try searching 4 'matrix.java' and c wat u get.

And wat's with the 20 questions? Don't no about u but I'm perplexed...

*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: How to transpose a matrix?

 
0
  #5
Jan 8th, 2006
iamthwee,


Originally Posted by iamthwee
If by linear u mean a 1 dimensional array, then yes thats possible. But to do so would B just stupid...
It is meaningful. It can save space (foot print) if we represent a matrix using one dimensional array and we need to transpose the matrix frequently.

Anyway, do you have any ideas?


regards,
George
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: How to transpose a matrix?

 
0
  #6
Jan 8th, 2006
no, you don't save a byte using a one dimensional array instead of a multidimensional array.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: How to transpose a matrix?

 
0
  #7
Jan 8th, 2006
jwenting,


Originally Posted by jwenting
no, you don't save a byte using a one dimensional array instead of a multidimensional array.
Why? I think any array (no matter of how much dimension it has) can be represented as one dimensional (linear) array. For example, we can create a method called element (i, j) to return the required element in a 2 dimensional array.


regards,
George
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: How to transpose a matrix?

 
0
  #8
Jan 9th, 2006
yes you can, but the 1 dimensional array would take up the same amount of space in memory as would the 2 dimensional array.

And that's without the added address space needed for the accessfunctions (and the slower access to the data using those functions instead of whatever optimisations the JVM may employ on arrays).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: How to transpose a matrix?

 
0
  #9
Jan 10th, 2006
jwenting,


Originally Posted by jwenting
And that's without the added address space needed for the accessfunctions (and the slower access to the data using those functions instead of whatever optimisations the JVM may employ on arrays).
Could you please provide more detailed descriptions of the above points? For example, "the added address space needed for the accessfunctions" and "optimisations the JVM may employ on arrays" please?


regards,
George
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: How to transpose a matrix?

 
0
  #10
Jan 10th, 2006
A method takes up space in memory.
A method call takes time in access to the instructions performed in that method.
The compiler may be able to optimise some instructions (like direct array access), making them a lot faster than doing the same through a method.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC