File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes drawing a matrix in Java -HELP!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "drawing a matrix in Java -HELP!!!" Watch "drawing a matrix in Java -HELP!!!" New topic
Author

drawing a matrix in Java -HELP!!!

Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
I need to draw a similarity matrix that holds a number of values. How do i do this? An example is provided below. The places where values need to be added are indicated by an x. I could specify which array elements must be empty ie, = to null but the matrix can change shape and thus needs to consistently enforce the below:

1 2 3 4
1 0
2 x 0
3 x x 0
4 x x x 0

Please help!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
What have you done so far to find the solution yourself? What problems have you encountered? Please provide some code that you have tried as well as compiler errors and output, if any.
Layne


Java API Documentation
The Java Tutorial
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
At the moment I am trying to do the matrix in an array. You see I am not very good at Java and don't knoiw much about. Maybe I shouldn't be using an array- if that is the case could you suggest some alternative structures please.
At the moment this is what I have come up with:

int row1= rsmd.getRowCount(), row2= rsmd.getRowCount();
int[][] distanceMatrix;
distanceMatrix= new int [row1][row2];
/*This produces a matrix with the same number of column as rows. The size of the matrix changes since it stores some calculations that have been carried out on database rows. As a result the number of rows that the database has is called for and this determines the size of the matrix.*/
My problems begin after this. I could say that if the row number is equal to the column number then place a 0 in this element.ie, for a 2*2 matrix if row number is 1 and column number is 1 then put a 0 there. (I don't know how to code this).
I could also say that if the column number(for the column that we are considering) is greater than the row number(for the row we are considering )then leave the element blank. Again I don't know how I would code this.
I could then enter the values into the remaining array elements. However, this is also difficult because all the calculation values are placed into an array (which then need to be put into the matrix). The fact that the size of the matrix varies depending on the database makes this difficult.
Please help- I really have no idea as to how I can do this.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
First, I have some suggestions for the code you have so far. I would use names like num_rows and num_cols instead of row1 and row2. Numbering your variables usually isn't very descriptive. Besides, only one of these numbers represent the rows in the matrix. The other is the columns.
As for initializing, you should consider using a for loop, or even a set of nested for loops, if necessary. I'm not sure what you mean by "leave the element blank." Since an array is stored in memory, it always has a value, even if it's meaningless. You could easily initialize ALL elements to some default value that you interpret as meaning "empty" or "blank" However, this depends on what valid the valid values for "non-blank" elements are. Also, the idea of "leaving an element blank" may not be pertinent if there are no possible operations between the different steps of initializing the array. I suspect this is what you really meant. You can fill in some elements (the diagonal with 0s) and then continue by filling in the remaining elements as appropriate. In this case, you just need to be careful that all the elements are set to a valid value before continuing any operations on the "matrix".
I think I'll stop there and let you work out some ideas of your own. Come back with more questions as you continue working on this.
Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: drawing a matrix in Java -HELP!!!