?
Solved

Matrix Lattice

Posted on 2011-05-01
36
Medium Priority
?
379 Views
Last Modified: 2012-06-21
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

Thanks in advance..

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

Thanks in Advance..
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);
			    		
			}
		}
	}

Open in new window

0
Comment
Question by:ZURINET
  • 17
  • 15
  • 4
36 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35500921
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;
    }
}

Open in new window

0
 

Author Comment

by:ZURINET
ID: 35501079
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35501113
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ZURINET
ID: 35501117
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;
    }

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35501124
So do you need a matrix which will have one value as each element or  vector of values?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35501129
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();

data[M][N].add(new Double(Math.random()));
}
}
0
 

Author Comment

by:ZURINET
ID: 35501137
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
 
LVL 47

Expert Comment

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

Author Comment

by:ZURINET
ID: 35501157
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35501175
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35501202
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);
al.add(cf1);}
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 Comment

by:ZURINET
ID: 35501221
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35501239
>>What I need is this

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

Expert Comment

by:for_yan
ID: 35501253
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 Comment

by:ZURINET
ID: 35501356
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();
        }
    }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35501424
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]));


   }

Open in new window

0
 

Author Comment

by:ZURINET
ID: 35501521
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35501542
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;
    }
}

Open in new window

0
 

Author Comment

by:ZURINET
ID: 35701459
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] + ")");

your help will be appreciated

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35701688
Should admit that I again lost your thought since we were talking about chess.
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();
    }

}

Open in new window



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

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35701774
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;
    }

}

Open in new window


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)

Open in new window

0
 

Author Comment

by:ZURINET
ID: 35701829
Hi Thanks

May I ask what does this function do?

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

Expert Comment

by:for_yan
ID: 35701873
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35701889
Sorry, Random class has such methkod for integers not Math
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35701905

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 Comment

by:ZURINET
ID: 35701908
Thanks you made my day (night).. will get a nice sleep now..
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35701923
You are always welcome - day and night :)
0
 

Author Comment

by:ZURINET
ID: 35701947
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35701961
What is Algorithm problem?
0
 

Author Comment

by:ZURINET
ID: 35701998
SOM : self organizing map..  Just need to work on a tutorial . as back ground .. to some finiancial solution I am working on
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35702053
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 Comment

by:ZURINET
ID: 35702065
Evething there is in Java..
Financial interest would not be a problem
0
 
LVL 47

Expert Comment

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

Author Comment

by:ZURINET
ID: 35702087
Thanks any way
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35702095
You are always welcome.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question