-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVector.java
More file actions
34 lines (30 loc) · 956 Bytes
/
Vector.java
File metadata and controls
34 lines (30 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.ArrayList;
import java.util.List;
public class Vector implements Comparable<Vector> {
ArrayList<Double> points;
Double classifier;
Double distance;
public Vector(List<Double> dataRow) {
this.points = new ArrayList<Double>();
for(int i = 0; i < dataRow.size() - 2; i ++) {
points.add(dataRow.get(i));
}
classifier = dataRow.get(dataRow.size() - 1);
}
/*Write a function that takes in a vector and returns the distance.*/
public void computeDistance(ArrayList<Double> vectorB) {
if(vectorB.size() != points.size())
System.out.println("Something is wrong with the vector sizes.");
Double dist = 0.0;
for(int i = 0; i < points.size(); i++) {
dist += Math.pow(points.get(i) - vectorB.get(i), 2);
}
distance = Math.sqrt(dist);
}
/*Implement a comparator on distance*/
@Override
public int compareTo(Vector o) {
// TODO Auto-generated method stub
return this.distance.compareTo(o.distance);
}
}