# Matrix Lattice

I have a matrix below..
I am looking for a way of Identifying every coordinate (X,Y) in the matrix Matrix(4,32)
That should be (128) unique Identifications..
For every Uniquie ID , I need to assigned or reference the ID with a uniqe double Vector [12.2, 1.2,9.4,1.4] or [1,4] matrix

A simple get and set method for the inner vector [1,4 matirx] is a bonus.. :-)

``````final public class Matrix {
private final int M;             // number of rows
private final int N;             // number of columns
private final double[][] data;   // M-by-N array

// create M-by-N matrix of 0's
public Matrix(int M, int N) {
this.M = M;
this.N = N;
data = new double[M][N];
}

// create matrix based on 2d array
public Matrix(double[][] data) {
M = data.length;
N = data[0].length;
this.data = new double[M][N];
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
this.data[i][j] = data[i][j];
}
}
********************************************
Accessible  through
Matrix myDouble = new Matrix(4,32);
myDouble.show();

******************************
I have tried this but it comsumes lots of resources

public Lattice(int w, int h) {
width = w;
height = h;
matrix = new Node[width][height];
float xstep = .5f / (float)width;
float ystep = .5f / (float)height;
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
matrix[x][y] = new Node(4);
matrix[x][y].setX(x);
matrix[x][y].setY(y);
// System.out.println(matrix.clone());
matrix[x][y].setWeight(0, (xstep * x) + (ystep * y));
matrix[x][y].setWeight(1, (xstep * x) + (ystep * y));
matrix[x][y].setWeight(2, (xstep * x) + (ystep * y));
matrix[x][y].setWeight(3, (xstep * x) + (ystep * y));

// System.out.println(matrix.length);

}
}
}
``````
###### Who is Participating?

Commented:
Or maybe something like that:
Explain please, more clearly waht you are looking for.

Code:

``````public class MatrixLattice {
public MatrixLattice(){

int M = 3;
int N = 2;
double[][]  map = new double[M][N];
RandomNode [][] nmap = new RandomNode[M][N];
for(int i =0; i<M; i++ ){
double f1 = Math.random();
int kk = (int)(f1*3.0);
for(int j=0; j<N; j++){

double f2 = Math.random();
int ll = (int) (f2*3.0);

System.out.println("Node " + i + ": (" + kk + " " + ll + ")");
nmap[i][j] = new RandomNode(kk,ll);

}
}

for(int i=0; i<M;i++){
for(int j=0; j<N; j++){
System.out.println("Node + [" + i + "][" +j + "]: " + nmap[i][j].toString());
}
}

}
public static void main(String [] args){
new MatrixLattice();
}

}

class RandomNode{
int i;
int j;

RandomNode(int i, int j){
this.i = i;
this.j = j;
}
public String toString(){
String s = "(" + i + " " + j + ")";
return s;
}

}
``````

Output:

``````Node 0: (0 2)
Node 0: (0 0)
Node 1: (1 1)
Node 1: (1 2)
Node 2: (1 1)
Node 2: (1 0)
Node + [0][0]: (0 2)
Node + [0][1]: (0 0)
Node + [1][0]: (1 1)
Node + [1][1]: (1 2)
Node + [2][0]: (1 1)
Node + [2][1]: (1 0)
``````
0

Commented:
I'm not 100% sure about what you're asking, but you could store a third dimension for the vector, e.g.
``````import java.util.*;
import java.io.*;

public class V {
public static void main(String[] args) {
int M, N;
M = N = 4;
final double[][][] data = new double[M][N][1];
for(int i = 0;i < M;i++) {
for(int j = 0;j < N;j++) {
data[i][j] = getRandomVector();
}
}
}

public static double[] getRandomVector() {
double[] vector = new double[4];
for(int i = 0;i < vector.length;i++) {
vector[i] = Math.random() * 10;
}
return vector;
}
}
``````
0

Author Commented:
Hi

What I need is this
final double[][] data = new double[M][N]
x and y coordinate of the position of [M][N] This will give 16 (different unique coordinates)

3               0.0000       0.0000    [0.5, 1.3  ,2.0  ,0.3]     0.0000
2               0.0000       0.0000    0.0000                         0.0000
1               0.0000       0.0000    0.0000                         0.0000
0               [0,1,2,3]     0.0000    0.0000                         0.0000

0              1             2             3

for Position (0,0) = reference another matrix or vector [0,1,2,3]
for Position (3,2) = reference another matrix or vector [0.5, 1.3  ,2.0  ,0.3]
0

Commented:
OK, you can have a matrix of vectors but all vectors
have ne element but for these two
which have four elemets

Wjat do you want to do wit this matrix?
0

Author Commented:
I already have a matrix that generates the needed random values

If I have the coordination of the matrix.. i can just assing or refernce or associate a matrix [1, 4 ]  to it

This is my problem
``````// create and return a random M-by-N matrix with values between 0 and 1
public static Matrix random(int M, int N) {
Matrix A = new Matrix(M, N);
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
A.data[i][j] = Math.random();
return A;
}
``````
0

Commented:
So do you need a matrix which will have one value as each element or  vector of values?
0

Commented:
I think you rather create

This will be matrix with vectors in ecah element
but all vectors will have one element representing random double number between 0 and 1
Do you want these vectors to be of random length?

final ArrayList [][] data;
data = new ArrayList[M][N];

for(int i=0; i<M;, i++){
for (int j=0; j<N; j++){
data{M][N] = new ArrayList();

}
}
0

Author Commented:
I need a matrix that will have 4 values at each element..

The code above is just an example.. it will take me more time to manuall feel every each with a value..

3               [7,2,3,4]       [2,2,3,4]    [0.5, 1.3  ,2.0  ,0.3]
2               [0,2,3,4]       [9,2,3,4]    [1,2,3,4]
1               [l,2,3,4]       [0,2,3,4]    [1,2,3,4]

0               [0,1,2,3]     [5,2,3,4]   [1,2,3,4]

0              1             2             3

Hence .. I can call coordinate.. (x,Y)  = (3,3) value =  [0.5, 1.3  ,2.0  ,0.3]
I need to get this coordinate  = or update it.. based on the position..
coordinate (0,0) =   [0,1,2,3]

0

Commented:
Are each of these four elemant random numbers?
Or you want to input them sya from file?
0

Author Commented:
I am getting them from a file.. ..
I need to populate 2D Lattice. with  values

Exampe Chess board game....

For every move a kings made.. there is a position.. or coordinate..
Hence the opponet will get the position..
Example for a starting position of a king..  I need to insert a [1,4] matirx inside this Position
0

Commented:
Then what you probably need is for each cell of your matrix you want to
have a vector of all matrix positions, which are accessible from this point - correct?

Then by the way for each chess figue you would cretae its own matrix,
as the kinfg will have one matrix, the queen will have another, the pawn will have third matrix,

Is this what you want?
0

Commented:
If so, you should create a class called say
a ChessField
and this class will have coordinates - two intgere numbers bewenn  0,1,...7
and methods
And metods should correspond to chess figure moves and each method will
have a return value of destnation field or even array of destination fields

so if I have a

ChessField cf = new ChessField(int m, int n);

ArrayList getKingFileds(ChessField cf){
//and here you produce array of those fileds
//which are accessible to king
//that is simle, say
ArrayList al = new ArrayList();
int x = cf.getX();
int x = cf.getY();
if(x>1){cf1 = new ChessField(x-1,y);
if(x<7)(...)
and so on...

}

Are you thinmkking about something like that?

and then you'll go through all positions and will generate vectos of positions
accessible for particular figure
Sounds like a good begiining for the chess program

0

Author Commented:
Hi yan
I am not doing Chess
But the discussion gave me the Aha.. moment..
In that I can use the same logic offered by chess..

Thanks for the input
0

Commented:
>>What I need is this

That's what my code will do - just change the 'vector' to have 16 elements instead of 4
0

Commented:
Great!
Actually, I also liked it - never thought before
about how they porogram chess; even though, I understand, that
you are not into chess.
Anyway, let us know if you have further questions

0

Author Commented:
Hi CEHJ

Do you have any Idea how I can output/unit test your code to see how the system is displaying the data

I have  matrix code that does just that for my class..

``````// print matrix to standard output
public void show() {
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++)
System.out.printf("%9.4f ", data[i][j]);
System.out.println();
}
}
``````
0

Commented:
Try
``````public void show() {
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
System.out.println(Arrays.toString(data[i][j]));

}
``````
0

Author Commented:
Thanks

I have to look for a new approach to solve the problem..
Cannot make a head way at the moment.. with the  suggestions....

Regards
0

Commented:
Works ok for me:
``````import java.io.*;

import java.util.*;

public class V {
static int M = 4;
static int N = 4;
final static double[][][] data = new double[M][N][1];

public static void main(String[] args) {

for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
data[i][j] = getRandomVector();
}
}

show();
}

public static void show() {
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
System.out.println(Arrays.toString(data[i][j]));
}

public static double[] getRandomVector() {
double[] vector = new double[4];

for (int i = 0; i < vector.length; i++) {
vector[i] = Math.random() * 10;
}

return vector;
}
}
``````
0

Author Commented:
I have a work around..
and need to just

Produce the output

Node 0: (0 0)
Node 1: (0 1)
Node 2: (1 0)
Node 3: (1 1)
Node 4: (2 0)
Node 5: (2 1)

for this matrix

``````int M = 3;
int N = 2;
double[][]  map = new double[M][N];

System.out.println("Node " + i + ": (" + map[i] + " " + map[i] + ")");

``````
0

Commented:
Perhaps this is waht you want:

``````public class MatrixLattice {
public MatrixLattice(){

int M = 3;
int N = 2;
double[][]  map = new double[M][N];
for(int i =0; i<M; i++ ){
double f1 = Math.random();
int kk = (int)(f1*3.0);
for(int j=0; j<N; j++){

double f2 = Math.random();
int ll = (int) (f2*3.0);

System.out.println("Node " + i + ": (" + kk + " " + ll + ")");

}
}

}
public static void main(String [] args){
new MatrixLattice();
}

}
``````

``````Node 0: (1 1)
Node 0: (1 1)
Node 1: (0 1)
Node 1: (0 0)
Node 2: (1 2)
Node 2: (1 0)
``````
0

Author Commented:
Hi Thanks

May I ask what does this function do?

int kk = (int)(f1*3.0); ??
0

Commented:
Sure.

double f1 = Math.random() generates double number from 0.0 to 1.0

if you write int kk = (int) f1;
then kk will alwys be zero becuase (int) does truncation
however if you mutiply by 3 then it will be 0 or 1 or 2 depending on
what random number we got in thne first place.

Math class has alos method which generates random integers below certain n (say below 3)
If you want you can use that one.
0

Commented:
Sorry, Random class has such methkod for integers not Math
0

Commented:

This is how you do it with integers:

Random r = new Random();
int ii = r.nextInt(3);

ii is >= 0 and <3 in this case.

0

Author Commented:
Thanks you made my day (night).. will get a nice sleep now..
0

Commented:
You are always welcome - day and night :)
0

Author Commented:
Hi for_Yan I am looking for a solution to a Algorithm problem .. I found on the web.. can you help me out?
I don't know if I am allowed to ask for such help here?
0

Commented:
What is Algorithm problem?
0

Author Commented:
SOM : self organizing map..  Just need to work on a tutorial . as back ground .. to some finiancial solution I am working on
0

Commented:
Sorry, I'm afraid I would hardly be of any help - I'm not specialist in algorithms.
I'm doing mostly Java programming
0

Author Commented:
0

Author Commented:
Evething there is in Java..
Financial interest would not be a problem
0

Commented:
Thank you, but you probably want to find someone closer, whom you could meet or at least talk.
0

Author Commented:
Thanks any way
0

Commented:
You are always welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.