[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

Vector distance from double to Matrix

Hi all ..
I have the method below which calculate the distance between 2 double variables.
How can I modify the method to be able to take a vector/Matirx in this format:
(2, 3, 3)
(2, 10, 3)  

Thanks in Advance
public static double varVector_distance(double[] geneVector, double[] genePosition){
	
		int m=Math.min(geneVector.length,genePosition.length);
		int n=Math.max(geneVector.length,genePosition.length);
		double sum=0;
		for(int i=0;i<n;i++)
			{
				double d=0;
				if(i<m)
					{
						d=geneVector[i]-genePosition[i];
					}
				else if(i<geneVector.length)
					{
						d=geneVector[i];
					}
				else
					{
						d=genePosition[i];
					}
				sum=sum+(d*d);
			}
	return Math.sqrt(sum);		
	
}

Open in new window

0
ZURINET
Asked:
ZURINET
  • 6
  • 5
  • 4
  • +1
1 Solution
 
for_yanCommented:
From any 2d array you cvan produce 1D array like you had it in your previous question
After that the task becomnes the same as this one
0
 
CEHJCommented:
The following will return double[] from a String in format "(2,3,4)"
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:


So do you want to have distance between twodimensional arrays of
arbotrary dimesnsions:
public static double varVector_distance(double[][] geneVector, double[][] genePosition) ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
for_yanCommented:
  geneVector = new double[M];
  genePosition = new double[K][L];

double [] genePosition1D = new double[K*L];
for (int j=0; j<K; j++) {
   for (int i=0; i<L; i++) {
      genePostion1D[j * L + i] = new genePostion[K][L];
   }
}

double distance = varVector_distance(geneVector, genePosition1D);


	
		int m=Math.min(geneVector.length,genePosition.length);
		int n=Math.max(geneVector.length,genePosition.length);
		double sum=0;
		for(int i=0;i<n;i++)
			{
				double d=0;
				if(i<m)
					{
						d=geneVector[i]-genePosition[i];
					}
				else if(i<geneVector.length)
					{
						d=geneVector[i];
					}
				else
					{
						d=genePosition[i];
					}
				sum=sum+(d*d);
			}
	return Math.sqrt(sum);		
	

Open in new window

0
 
for_yanCommented:
Sorry you don't need the rest:

 geneVector = new double[M];
  genePosition = new double[K][L];

double [] genePosition1D = new double[K*L];
for (int j=0; j<K; j++) {
   for (int i=0; i<L; i++) {
      genePostion1D[j * L + i] = new genePostion[K][L];
   }
}

double distance = varVector_distance(geneVector, genePosition1D);

Open in new window

0
 
CEHJCommented:
>>So do you want to have distance between twodimensional arrays of
arbotrary dimesnsions

This is what is required:

>>
How can I modify the method to be able to take a vector/Matirx in this format:
(2, 3, 3)
>>
0
 
objectsCommented:
Isn't that just repeating what I had already posted ?

0
 
ZURINETAuthor Commented:
Not really..
I have mutiple data format .. I will be feeding into the Cells
The system should be capable enough of taking in Heuristic dimensions;;

Expected formats

[2, 1, 0] int
[2.0, 1.2, 0.0] double
[4;9;9] int
0
 
objectsCommented:
sorry was referring to the code that for_yan posted. Appears to just copy what you already have
0
 
CEHJCommented:
>>Expected formats

Well, those are different ;) What i posted should work for what you posted (originally). Easy enough to adapt
0
 
CEHJCommented:
In fact, simply the following should do it:
String[] atoms = v.replaceAll("[\\[\\]() ]", "").split("[;,]");

Open in new window

0
 
ZURINETAuthor Commented:
Hi .. CEHJ

I am doing a test run on the code..
I will give my feedback later..
0
 
ZURINETAuthor Commented:
Hallo for_yan
This multiplication? will it not destory the value of the vector?
double [] genePosition1D = new double[K*L];
0
 
CEHJCommented:
:)
0
 
for_yanCommented:
ZURINET,

I'd suggest that next time you post the question, please try to read it and
figure out what a person who cannot read your thoughts, but can read only
what you post,  would understand from your posting.

Those are not questions which you post, those are riddles, where the main thing is to figure out
what you meant.

In the previous question you posted lengthy code full of java inconsistencies and several questions
in the comments  inside, about different issues (and I actually fixed several of them),
 whereas your question, as it turned out, was in fact how to make one-dimensional array from two-dimensional
array.
 
Now read the riddle at the top of this trail:
"I have the method below which calculate the distance between 2 double variables. "
Below is the method which calculates the distance between two arrays, not just two variables
and these arrays are of different lengths. That's fine, perhaps you just made a misprint,
typing variable instead of array.

Your question is
"How can I modify the method to be able to take a vector/Matirx in this format:"
Well, after I read it, my assumtion was that instead of having distance between two arrays,
you want now distance between array and matrix (two-dimensional array).

True, I could not explain to myseklf how your triplets, which you showed below could  play into it.

Now the solution - as we now know, it is nothing more than splitting the String which contains triplet into 3 numbers of type double.
If you needed to know how to get three numbers of type double from the string of
the type "(3,3,2)", then why would you need to post this long method with the distance between arrays?

So, one thing I'd still appreciate if you explained to me, is  how would you use this
method together with splitting of these triplets. I'm not that smart -I even now still cannot figure out that - and how
it will take vector/matrix in this case - please explain to satisfy my curiosity.

The other thing is more general, and I know you absolutely do not need to listen to my advice,
stil,l who knows, maybe you'll hear it, the I would be happy: just try to read what you are asking with the eyes of a person who
was not concerned with this question for previous days/weeks, who reads something new to him.
 Write more extended explantions, even don't be afraid to repeat yourself in some difficult questions,
which are relevant to your question.
On the other hand, and even more importantly, try to formulate what is in fact your question, don't put
irrelevant stuff, which distracts attention (like the long piece of code in the previous
question). Believe me, a little bit of thinking in such case would pay off, and not only on EE.




 
0
 
ZURINETAuthor Commented:
Hi for_yan

Your comment is accepted as written..

Accept my apologies

Regards

ZS
0
 
for_yanCommented:
No apologies really needed, just wanted to share a small piece of experience.
I know it is difficult to know how other people will understand what you are writing,
we are all struggling with it.
Good luck with future questions.
0

Featured Post

Industry Leaders: 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!

  • 6
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now