Solved

# Vector distance from double to Matrix

Posted on 2011-05-09
305 Views
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
Question by:ZURINET

LVL 47

Expert Comment

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

LVL 86

Accepted Solution

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

LVL 47

Expert Comment

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

LVL 47

Expert Comment

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

LVL 47

Expert Comment

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

LVL 86

Expert Comment

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

LVL 92

Expert Comment

0

Author Comment

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

LVL 92

Expert Comment

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

LVL 86

Expert Comment

>>Expected formats

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

LVL 86

Expert Comment

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

Author Comment

Hi .. CEHJ

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

Author Comment

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

LVL 86

Expert Comment

:)
0

LVL 47

Expert Comment

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 Comment

Hi for_yan

Your comment is accepted as written..

Accept my apologies

Regards

ZS
0

LVL 47

Expert Comment

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

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 …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…