Results 1 to 14 of 14
Thread: Linearizing a 2D Array/Matrix
 03022009, 06:42 PM #1Member
 Join Date
 Jan 2009
 Posts
 22
 Rep Power
 0
Linearizing a 2D Array/Matrix
Alright, I need to write two methods, the first of which linearizes a matrix, such as 4x6. I need to convert it into 1 x (4*6). So the number of rows becomes one and the columns are the product of the dimensions of the matrix.
So a 3x3 would revert into a 1x9 where all the elements in each index were in their own column.
 1 1 1 
 2 2 2 
 3 3 3 
would need to convert to:
 1 1 1 2 2 2 3 3 3 
This is what I have so far for the linearize method.
Java Code:public Matrix linearize() { Matrix result = new Matrix(); int rows = 1; int columns = this.getColumns() * this.getRows(); result.theArray = new int[rows][columns]; for( int r = 0; r < result.getRows(); r++ ) for( int c = 0; c < result.getColumns(); c++ ) { result.theArray[r][c] = theArray[c].length; } return result; }
Java Code:result.theArray[r][c] = theArray[c].length;
Thanks for any help.
 03022009, 06:49 PM #2
Several approaches are available, keep going with what you have. Start with a dual approach of having a list or something from the Collections and do append then toArray and cast to the array type you need.
The other, which is where you are going, is doen by allocation a new array as int[] anArray = new int [ ( numRow * numColumn) ]; then just tediously work the loop logic until you get something that lists correctly on an array walk.
Easy to miss for several test cycles, then if falls like rain.
The entire information empire is rooted in which way the loop runs, head first or feet first.
Don't buy any books to solve this, just work it till it works.
Be sure to write some test harness.Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
 03022009, 07:02 PM #3Member
 Join Date
 Jan 2009
 Posts
 22
 Rep Power
 0
Forgive me for sounding rather green, (while I am indeed green), as I have only been in the Java world for about 8 months now. I do know that I cannot call to any other methods outside the class such as .append (while ive seen them myself). The approach that I am currently taking is the one ill be using, I just cant work around that last bit of code, where i need to fill the array.
Java Code:Matrix C(5, 2):  3 0   1 3   1 0   4 3   0 1 
And this method approach so far has gotten me:
Java Code:Matrix V(1, 10):  3 0 0 0 0 0 0 0 0 0 
Java Code:public Matrix linearize() { Matrix result = new Matrix(); int rows = 1; int columns = this.getColumns() * this.getRows(); result.theArray = new int[rows][columns]; [COLOR="blue"]for( int r = 0; r < result.getRows(); r++ ) for( int c = 0; c < this.getColumns(); c++ )[/COLOR] { [COLOR="Blue"]result.theArray[r][c] = this.theArray[r][c];[/COLOR] } return result; }
Where is the fault in my loop, as I assume its there.
Due by midnight CST, this is the last step, Would appreciate any solutions.
UPDATE: Still very much stuck.Last edited by dalangley; 03022009 at 07:43 PM.
 03022009, 07:55 PM #4Member
 Join Date
 Jan 2009
 Posts
 22
 Rep Power
 0
Any help at all? Google seems to be useless as I cant find anything on this particular problem I am having.
 03022009, 08:02 PM #5
you will need to so something similar to this:
Java Code:int k = 1; for(int j=0; j<matrix.length; ++j){ for(int i=0; i<matrix[j].length; ++i){ flat[++k] = matrix[j][i]; } }
USE CODE TAGS> [CODE]...[/CODE]
Get NotePad++ (free)
 03022009, 08:19 PM #6Member
 Join Date
 Jan 2009
 Posts
 22
 Rep Power
 0
Java Code:public Matrix linearize() { int data = this.getColumns() * this.getRows(); int[] result = new int[data]; int k = 1; for( int j = 0; j < this.theArray.length; j++ ) for( int i = 0; i < this.theArray[j].length; i++ ) { result[++k] = this.theArray[j][i]; } return result; }
Java Code:Exception in thread "main" java.lang.RuntimeException: Uncompilable source code  incompatible types found : int[] required: Matrix
What do i need to change to get the result to get through? Do i have the wrong type somewhere?
 03022009, 08:32 PM #7
the error msg tells all.
change return type from Matrix to int[]. or,
change result in loop to:
result.theArray[r][++k] =USE CODE TAGS> [CODE]...[/CODE]
Get NotePad++ (free)
 03022009, 11:02 PM #8Member
 Join Date
 Jan 2009
 Posts
 22
 Rep Power
 0
Java Code:public Matrix linearize() { int data = this.getColumns() * this.getRows(); int[] result = new int[data]; int k = 1; for( int j = 0; j < this.theArray.length; j++ ) for( int i = 0; i < this.theArray[j].length; i++ ) { result[++k] = this.theArray[j][i]; } return result; }
A.linearize();
Where A is the 2D array or Matrix. So A is a Matrix object being passed into a method that needs to return a 1D Array or Flat Array. I am following your logic but the code won't work in its current state. I believe it is a matter of the method type, but either way I switch it it throws the error. If I make it Matrix type as seen above, the 1D array "result" is not able to be a return type. This is rather frustrating, and I am sure its something simple. Any ideas? Or a reference to a method elsewhere?
 03022009, 11:11 PM #9Member
 Join Date
 Jan 2009
 Posts
 22
 Rep Power
 0
Java Code:public Matrix linearize() { Matrix result = new Matrix(); int rows = 1; int columns = this.getColumns() * this.getRows(); result.theArray = new int[rows][columns]; for( int r = 0; r < result.getRows(); r++ ) for( int c = 0; c < this.getColumns(); c++ ) { result.theArray[r][c] = this.theArray[r][c]; } return result; }
Java Code:Matrix C(5, 2):  3 0   1 3   1 0   4 3   0 1 
and it linearizes yes, but it doesn't completely fill it:
Java Code:Matrix V(1, 10):  3 0 0 0 0 0 0 0 0 0 
So as you can see it is doing the first 2 indexes but not the rest, where in the for loop does this need to change? or is it in the result?
 03022009, 11:32 PM #10Member
 Join Date
 Jan 2009
 Posts
 22
 Rep Power
 0
Java Code:for( int r = 0; r < result.getRows(); r++ ) [COLOR="Blue"]for( int c = 0; c < this.getColumns(); c++ )[/COLOR] { result.theArray[r][c] = this.theArray[r][c]; } return result;
 03022009, 11:52 PM #11Member
 Join Date
 Jan 2009
 Posts
 22
 Rep Power
 0
Any ideas? This has had me stumped for hours now, Im hitting a brick wall on what I need to change in the for loop to make it iterate through ALL of the data in this.theArray .
 03032009, 12:42 AM #12Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 15
Java Code:for( int r = 0; r < result.getRows(); r++ ) { for( int c = 0; c < this.getColumns(); c++ ) { result.theArray[r][c] = this.theArray[r][c]; } } return result;
But in your code you assign to it with result.theArray[r][c] where c<this.getColumns().
Edit...
On rereading the thread, I see that you have already been given code that iterates through the source matrix. (It uses the number of rows and columns in the source matrix to do so).
Of the code you have now (rather than that suggested by replies #5 and #7) you say:
I went back to this method body for multiple reasons, the first being that it throws no error whatsoeverLast edited by pbrockway2; 03032009 at 12:52 AM.
 03032009, 01:07 AM #13
what i meant in reply #7 is:
going back to your original code...
Java Code:public Matrix linearize() { Matrix result = new Matrix(); int rows = 1; int columns = this.getColumns() * this.getRows(); result.theArray = new int[rows][columns]; [B] int k = 1; for( int j = 0; j < this.theArray.length; j++ ) for( int i = 0; i < this.theArray[j].length; i++ ) { result.theArray[0][++k] = theArray[j][i].length; }[/B] return result; }
that was what the error msg was complaining about.Last edited by angryboy; 03032009 at 03:39 AM.
USE CODE TAGS> [CODE]...[/CODE]
Get NotePad++ (free)
 03032009, 01:26 AM #14Member
 Join Date
 Jan 2009
 Posts
 22
 Rep Power
 0
Java Code:public Matrix linearize() { Matrix result = new Matrix(); int rows = 1; int columns = this.getColumns() * this.getRows(); result.theArray = new int[rows][columns]; for( int r = 0; r < result.getRows(); r++ ) for( int c = 0; c < this.getColumns(); c++ ) { result.theArray[r][c] += this.theArray[r][c]; } return result; }
Java Code:for( int r = 0; r < result.getRows(); r++ ) for( int c = 0; c < this.getColumns(); c++ )
Similar Threads

Help with dox matrix printer
By Albert in forum Advanced JavaReplies: 7Last Post: 09062011, 08:50 AM 
displaying 2DMatrix
By srinivasmallabathula in forum New To JavaReplies: 2Last Post: 02182009, 08:19 PM 
[SOLVED] Help with Matrix
By Bernard Robitaille in forum JCreatorReplies: 10Last Post: 02142009, 03:19 AM 
Matrix variations with repetition
By jnovice in forum New To JavaReplies: 4Last Post: 01192009, 09:42 AM 
Help with matrix
By susan in forum New To JavaReplies: 1Last Post: 08072007, 04:37 AM
Bookmarks