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

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

# 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)

``````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);

}
``````
0
ZURINET
• 6
• 5
• 4
• +1
1 Solution

Commented:
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

Commented:
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;
}
``````
0

Commented:

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

Commented:
``````  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);

``````
0

Commented:
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);
``````
0

Commented:
>>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

Commented:

0

Author 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

Commented:
sorry was referring to the code that for_yan posted. Appears to just copy what you already have
0

Commented:
>>Expected formats

Well, those are different ;) What i posted should work for what you posted (originally). Easy enough to adapt
0

Commented:
In fact, simply the following should do it:
``````String[] atoms = v.replaceAll("[\\[\\]() ]", "").split("[;,]");
``````
0

Author Commented:
Hi .. CEHJ

I am doing a test run on the code..
I will give my feedback later..
0

Author Commented:
Hallo for_yan
This multiplication? will it not destory the value of the vector?
double [] genePosition1D = new double[K*L];
0

Commented:
:)
0

Commented:
ZURINET,

I'd suggest that next time you post the question, please try to read it and
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,

"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

Author Commented:
Hi for_yan

Your comment is accepted as written..

Accept my apologies

Regards

ZS
0

Commented:
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

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