After that the task becomnes the same as this one

Solved

Posted on 2011-05-09

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

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

17 Comments

After that the task becomnes the same as this one

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

So do you want to have distance between twodimensional arrays of

arbotrary dimesnsions:

public static double varVector_distance(double[

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

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

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)

>>

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

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

```
String[] atoms = v.replaceAll("[\\[\\]() ]", "").split("[;,]");
```

This multiplication? will it not destory the value of the vector?

double [] genePosition1D = new double[K*L];

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.

Title | # Comments | Views | Activity |
---|---|---|---|

wordsWithoutList challenge | 24 | 61 | |

Safari, Firefox, IE, Java, Plugin, Java Security error | 13 | 26 | |

mapShare challenge | 13 | 41 | |

rhino java, javascript, converting asc code to character value, but the compiler keeps complaining that "charCodeAt" is not found ! | 3 | 21 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**22** Experts available now in Live!