Link to home
Start Free TrialLog in
Avatar of ZURINET
ZURINET

asked on

Matrix value assingment

Hi all

I need to assing a value to an array instance..
See attached code for problem definition..    
Class Cells
public Cells(int x, int y, int V_size){
	this.x = x;
	this.y = y;
	W_vector = new double[V_size];
	geneLs = new LinkedList<Gene>(); // not much consideration need
	
}
Cells[] retunMap; // this should be return to the calling instance
Cells[] map; //I created a instance of Cells class
map = new Cells[Q];  // I initialize it to ;; int Q = 6

// I assing some values to the vectors
for(int i = 0; i < map.length; 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);
					
				    map[i] = new Cells(M, N, vectorMySize);
					map[i].x = kk; // assing the value of kk to position x: This is the source of the problem
					map[i].y = ll; // assign the value of ll to position y:: This is the source of the problem
				    map[i].set_weight_vector(vsize); // this is not important
			}
		}
		
// assign the value of return map
this.retunMap = map;
		
I need to be able to print out the value of the map with this code
System.out.println("Coordinates for a 3 by 2 map:");
		for(int i=0; i<map.length; i++){
			
			System.out.println("Node " + i + ": (" + map[i].get_x() + " " + map[i].get_y() + ")");
		}
		
The code for get method is as follows
public int get_x(){
		return this.x;
	}
	
	public int get_y(){
		return this.y;
	}
	
// My result at the moment
// Note the Node value 	
Node 0 0 Node 1 1
weight vector: 0.00 0.00 0.00 
Genes:


Node 0 1 Node 1 1
weight vector: 0.00 0.00 0.00 
Genes:

//Expected output
Node 0 1
weight vector: 0.00 0.00 0.00 
Genes:

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

Well, the statement of your problem does not seem quite clear.
You show your result at the moment,
but your code isn't even close to be compiled.

Perhaps it would be better if you post a real code with comments
in the form of real comments
In this code say from the very beginning you
have this piece:


Class Cells
public Cells(int x, int y, int V_size){
      this.x = x;
      this.y = y;
      W_vector = new double[V_size];
      geneLs = new LinkedList<Gene>(); // not much consideration need
      
}

Even when I corrected the case for say Class
there is such problem that youj first need to declare instatnce variables x and y
and then have them assigned in the constructor:
say
public class Cells {
int x;
int y;

public Cells(int x, int y, int V_Size) {
this.x=x;
this.y=y;
double []W_vector = new double[V_size];
...
}

Open in new window


No surprise beow you have
map[i].x , map[i].y

Open in new window

,

where you write "this is the source of the problem" -
it complains because Cells do not have
declared fields x and y

In short, there are many issues in this code ,
so "see attahed code for problem definition"
does not work very well.
If that would be the code close
to ready, then it would have been possible to state the question this way,
but not in this case.


Avatar of ZURINET
ZURINET

ASKER

Hoi
Given that I have the variables in class Cells declared
however the values being stored in map coordinates are not uniqe..

To keep the question short, I am looking for a way to store
1D (dimenstion) array of M*N nodes; with the Nodes storing as follows

Node 0: (0 0)
weight vector: 0.00 0.00 0.00
Genes

Node 1: (0 1)
weight vector: 0.00 0.00 0.00
Genes
Node 2: (1 0)
weight vector: 0.00 0.00 0.00
Genes
Node 3: (1 1)
weight vector: 0.00 0.00 0.00
Genes
Node 4: (2 0)
weight vector: 0.00 0.00 0.00
Genes
Node 5: (2 1)

Node 0 0
weight vector: 0.00 0.00 0.00
Genes:


map[i].y = ll;
and map[i].x = kk;  are not uniqe..

Open in new window

You can use ArrayList and use your Cell class which will conatin x and y
ArrayList al = new ArrayList();
Cell c = new Cell(2,1...);<---whatever else you need in the constructor
al.add(c);

Cell c = new (0,1...);
al.add(c);

and so on, you can have aloop adding them

Then you should have methods which retrive x and y from
instance of your Cell

and then you can go through your arraylist
retirive each element and print your x and y or do with them whatever you
want.
In this case x any  do not need to be uique, you can store any number of cells with equal x and y
in your arraylist
You of couse don't need to declare variable "c" twice , so in my
sketch above second time I emasnt you'll say just

c =newCell(0,1...)

not

Cell c = ...
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ZURINET

ASKER

Hi objects..

I would love to send you some swiss chocolates :-)

You are the Guru!
Thanks a lot..
I love chocolate, and the swiss are pretty good at making it :)

Glad I could help. Have just hired another Java tutor so should have more time available now to help with your questions.