Browse Source

String to matrix function is finished.

featureMatrixCalculator
Lukas Reichwein 5 years ago
parent
commit
023b0dfe38
  1. 11
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java
  2. 47
      src/test/java/com/ugsbo/matrixcalc/StringToMatrixTest.java

11
src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java

@ -232,20 +232,21 @@ public class MatrixCalcMath {
ArrayList<String[]> singleNumbersArr = new ArrayList<String[]>();
//Splitting the strings into thier rows
//Splitting the strings into their rows
String[] singleNumbers = null;
String[] rows = stringMatrix.split("\n");
for (int i = 0; i < rows.length; i++) {
System.out.println(rows[i]);
//System.out.println(rows[i]);
//Splitting rows into their Numbers
singleNumbers = rows[i].split("\\s");
singleNumbersArr.add(singleNumbers);
}
int rowCount = rows.length; //row.length
int rowlength = singleNumbersArr.get(0).length; //row.length
int columCount = singleNumbersArr.size(); //output.length
double[][] result = new double[columCount][rowCount];
double[][] result = new double[columCount][rowlength];
for (int columIndex = 0; columIndex < singleNumbersArr.size(); columIndex++) {
for (int columIndex = 0; columIndex < columCount; columIndex++) {
for (int rowIndex = 0; rowIndex < singleNumbers.length; rowIndex++) {
result[columIndex][rowIndex] = Double.parseDouble(singleNumbersArr.get(columIndex)[rowIndex]);
}

47
src/test/java/com/ugsbo/matrixcalc/StringToMatrixTest.java

@ -36,6 +36,51 @@ public class StringToMatrixTest {
double[][] result = matrixMath.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
assertArrayEquals("The first row is not correct", expected[1], result[1], 0.1);
assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
}
@Test
public void StringWithSixDigitNumbersToMatrix_Returns1by6Matrix() {
String inputString = "1 2 3 4 5 6";
double[][] expected = { { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 } };
double[][] result = matrixMath.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
}
@Test
public void StringWithSixDigitNumbersToMatrix_Returns2by3Matrix() {
String inputString = "1 2 3\n4 5 6";
double[][] expected = { { 1.0, 2.0, 3.0}, { 4.0, 5.0, 6.0 } };
double[][] result = matrixMath.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
}
@Test
public void StringWithSixDigitNumbersToMatrix_Returns3by2Matrix() {
String inputString = "1 2\n3 4\n5 6";
double[][] expected = { { 1.0, 2.0}, { 3.0, 4.0}, { 5.0, 6.0 } };
double[][] result = matrixMath.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
assertArrayEquals("The therd row is not correct", expected[2], result[2], 0.1);
}
@Test
public void StringWithNineDigitNumbersToMatrix_Returns3by3Matrix() {
String inputString = "1 2 3\n4 5 6\n7 8 9";
double[][] expected = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }, { 7.0, 8.0, 9.0 } };
double[][] result = matrixMath.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
assertArrayEquals("The thierd row is not correct", expected[2], result[2], 0.1);
}
}
Loading…
Cancel
Save