| | |
How to transpose a matrix?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Unless the matrix is square you won't be able to handle that, as arrays are immutable in size.
Can't be put in the same array as
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).
Java Syntax (Toggle Plain Text)
| 1 2 3 | | 4 5 6 | == [[1,2,3],[4,5,6]]
Java Syntax (Toggle Plain Text)
| 1 4 | | 2 5 | | 3 6 | == [[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.
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Solved Threads: 0
jwenting,
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
•
•
•
•
Originally Posted by jwenting
Unless the matrix is square you won't be able to handle that, as arrays are immutable in size.
Can't be put in the same array asJava Syntax (Toggle Plain Text)
| 1 2 3 | | 4 5 6 | == [[1,2,3],[4,5,6]]
Java Syntax (Toggle Plain Text)
| 1 4 | | 2 5 | | 3 6 | == [[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).
Anyway, if you agree with that, do you have any ideas?
regards,
George
•
•
•
•
I think it is possible to represent a matrix by a linear array
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*
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Solved Threads: 0
iamthwee,
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
•
•
•
•
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...
Anyway, do you have any ideas?
regards,
George
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Solved Threads: 0
jwenting,
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
•
•
•
•
Originally Posted by jwenting
no, you don't save a byte using a one dimensional array instead of a multidimensional array.
regards,
George
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).
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.
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Solved Threads: 0
jwenting,
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
•
•
•
•
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).
regards,
George
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.
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.
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Java Phone accessing AddressBook
- Next Thread: Installing java files...
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu update windows






