# drawing a matrix in Java -HELP!!!

Naf Rash

Ranch Hand

Posts: 85

posted 11 years ago

- 0

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!

1 2 3 4

1 0

2 x 0

3 x x 0

4 x x x 0

Please help!

Layne Lund

Ranch Hand

Posts: 3061

Naf Rash

Ranch Hand

Posts: 85

posted 11 years ago

- 0

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.

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

Posts: 3061

posted 11 years ago

- 0

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

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

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |