• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

Compare double information

Hi all
I have an array initalized as double.. see attached code..
Given double v1 stored in a gene.. = See section A

And given three different vectors/double stored in a map; which is a container of (3 different vectors).
These includes: Node; weight; Genes:

See section B.

Already I have a method that compare values:: myComparator (double[] a, double[] b)

My only problem or question is this!
How can I extract a vector  (double[] v1 = {184, 188, 49};)
from a..
And How can I extract a vector  weight vector: from Node: 01( 179.00 159.00 63.00 )
from b

And present both to myComparator???

If you need more info please let me know..
***************************************************************************************
Section A
***************************************************************************************

                  double[] v1 = {184, 188, 49};
		double[] v2 = {183, 170, 7};
		double[] v3 = {199, 77, 158};

								
		Cell[] cells = new Cell[5];
		cells[0] = new Cell("g1", v1);
		cells[1] = new Cell("g2", v2);
		cells[2] = new Cell("g3", v3);

		Cell g1 = cells[0];
		Cell g2 = cells[1];		
		Cell g3 = cells[2];
***************************************************************************************
Section B
***************************************************************************************

		Node[] map = contained the following info when initialized
		(
		Node 0 0
		weight vector: 119.00 159.00 63.00 
		Genes:
		)
		
		(
		Node 0 1
		weight vector: 179.00 159.00 63.00 
		Genes:
		)
		
		(
		Node 10
		weight vector: 166.00 159.00 63.00 
		Genes:
		)

Open in new window

0
ZURINET
Asked:
ZURINET
  • 5
  • 2
2 Solutions
 
for_yanCommented:
You already had this solution from CEHJ for one of the previous questions,
you can add {} and I hope it should work for both cases with parentheses and braces
(let me check now):
public static double[] vectorize(String v) {
	String[] atoms = v.replaceAll("[() {}]", "").split(",");
	double[] result = new double[atoms.length];
	for(int i = 0;i < atoms.length;i++) {	
	    result[i] = Double.valueOf(atoms[i]);
	}
	return result;
    }

Open in new window

0
 
for_yanCommented:

Yes this same code  works both for parenthesis and braces
                   String v = "(184, 188, 49)";
	String[] atoms = v.replaceAll("[() {}]", "").split(",");
	double[] result = new double[atoms.length];
	for(int i = 0;i < atoms.length;i++) {
	    result[i] = Double.valueOf(atoms[i]);
         System.out.println(result[i] + " ");
	}

Open in new window


output:

184.0 
188.0 
49.0 

Open in new window

0
 
ZURINETAuthor Commented:
May be I just need a break...
up for more than 14 hours.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:
No sorry, that my code only works with commas.
Don't have time now.
I guess CEHJ will send you the right one for both.
0
 
for_yanCommented:
Ok this is simple and works for everything:

           //  String v = "{ 179.00 159.00 63.00 }";

        String v = "( 188, 144, 14)";
        StringTokenizer t = new StringTokenizer(v,"{}(), ");

	double[] result = new double[3];
        result[0] = Double.parseDouble(t.nextToken());
            result[1] = Double.parseDouble(t.nextToken());
            result[2] = Double.parseDouble(t.nextToken());
	for(int i = 0;i < result.length;i++) {

         System.out.println(result[i] + " ");
	}

Open in new window

0
 
for_yanCommented:
input:

( 188, 144, 14)

Open in new window



Output:
188.0 
144.0 
14.0 

Open in new window



input:

{ 179.00 159.00 63.00 }

Open in new window


output:


179.0 
159.0 
63.0 

Open in new window


0
 
objectsCommented:
you should just need the following
nice and simple, and it'll parse both formats
public static double[] parseVector(String s) {
		double[] array = new double[3];
		int i = 0;
		Scanner scanner = new Scanner(s.substring(1));
		while (scanner.hasNextDouble()) {
			array[i++] = scanner.nextDouble();
		}
		return array;
	}

Open in new window

0
 
objectsCommented:
> And present both to myComparator???

VectorComparator comparator = new VectorComparator();
int compare = comparator.compare(parseVector("( 188, 144, 14)"), parseVector("{ 179.00 159.00 63.00 }
"));
0

Featured Post

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.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now