Created
April 25, 2019 10:18
-
-
Save horitaku1124/0e60b205a0fd5560822f24b4ed71e282 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestVec1 { | |
private static int[] strToVec(String str, int len) { | |
int[] vec = new int[len]; | |
for (int i = 0;i < len;i++) { | |
if (i < str.length()) { | |
vec[i] = str.charAt(i); | |
} else { | |
vec[i] = 0; | |
} | |
} | |
return vec; | |
} | |
private static void printVec(int[] vec) { | |
System.out.print("["); | |
if (vec.length > 0) { | |
System.out.print(vec[0]); | |
for (int i = 1;i < vec.length;i++) { | |
System.out.print(", " + vec[i]); | |
} | |
} | |
System.out.println("]"); | |
} | |
private static float calcDistance(int[] vec1, int[] vec2) { | |
float sum = 0; | |
for (int i = 0;i < vec1.length;i++) { | |
sum += Math.pow(vec1[i] - vec2[i], 2); | |
} | |
return sum / vec1.length; | |
} | |
public static void main(String[] args) { | |
String str1 = "abc"; | |
String str2 = "adc"; | |
String str3 = "track"; | |
int[] vec1 = strToVec(str1, 5); | |
int[] vec2 = strToVec(str2, 5); | |
int[] vec3 = strToVec(str3, 5); | |
printVec(vec1); | |
printVec(vec2); | |
printVec(vec3); | |
System.out.println(calcDistance(vec1, vec2)); | |
System.out.println(calcDistance(vec1, vec3)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment