2016-03-24 8 views
-1

私はFindBestMatchというメソッドを持っていて、それは10回繰り返されるcompareMatricesという別のメソッドを呼び出します。 compareMatricesは、基本的に2つの配列を比較して数値を返す数学を行いますが、2つの配列が一致するほど数値が大きくなります。 FindBestMatchは、最も高い番号の値を返す必要があります。各番号の数値を返すメソッドのグループから最大の番号を見つける

public String FindBestMatch() { 

    compareMatrices(numFiles.getMatrix("zero"), imgMatrix); 
    compareMatrices(numFiles.getMatrix("one"), imgMatrix); 
    compareMatrices(numFiles.getMatrix("two"), imgMatrix); 
    compareMatrices(numFiles.getMatrix("three"), imgMatrix); 
    compareMatrices(numFiles.getMatrix("four"), imgMatrix); 
    compareMatrices(numFiles.getMatrix("five"), imgMatrix); 
    compareMatrices(numFiles.getMatrix("six"), imgMatrix); 
    compareMatrices(numFiles.getMatrix("seven"), imgMatrix); 
    compareMatrices(numFiles.getMatrix("eight"), imgMatrix); 
    compareMatrices(numFiles.getMatrix("nine"), imgMatrix); 

} 

そこで、基本的FindBestMatch実行、9を通じてゼロ、およびそれぞれの時間は数を返します。

は、ここで私が持っているものです。 compareMatricesコールが最も高い数値を返す方法を知る方法がわかりませんか?

EDIT

ここに「compareMatrices」があります。数値ファイルの2D配列と画像ファイルの2D配列を入力として受け取ります。

public double compareMatrices(int[][] num, double[][] img) { 
    int nNumRows = num.length; 
    int nNumCols = num[0].length; 
    int nImgRows = img.length; 
    int nImgCols = img[0].length; 

    double highest = 0; 

    for (int row = 0; row < nImgRows - nNumRows + 1; row++) { 
     for (int col = 0; col < nImgCols - nNumCols + 1; col++) { 
      double score = 0; 
      for 
      (int row_offset = 0; row_offset < nNumRows; row_offset++) { 
       for (int col_offset = 0; 
         col_offset < nNumCols; col_offset++) { 
        int imgRowIndex = row + row_offset; 
        int imgColIndex = col + col_offset; 
        int numV = num[row_offset][col_offset]; 
        double imgV = img[imgRowIndex][imgColIndex]; 

        if ((imgV == 1) && (numV == 1)) { 
         score +=1; 
        } else if ((numV == 1) & (imgV == 0)) { 
         score -= 0.25; 
        } else if ((numV == 0) && (imgV == 0)) { 
         score += 0.25; 
        } 
       } 
      } 

      if (score > highest) { 
       highest = score; 
      } 
     } 
    } 
    return highest; 
} 

そして、それは基本的に数行列と画像マトリックスマッチより良い、より高いスコア、スコアを返します。

+0

'compareMatrices'は何をスコアとする方法は、文字列を返します?それは何かを返しますか?その入力は何ですか? –

+0

さて、変数のように戻り値をどこかに格納してから、別のものと比較することから始めることができます。 – azurefrog

答えて

0

私はあなたが配列String[]に文字列を格納し、最大値を見つけるために、forループを使用することを示唆している:

public String FindBestMatch() { 
    String[] m = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; 
    double max = compareMatrices(numFiles.getMatrix(m[0]), imgMatrix); 
    double score = 0; 
    int maxIndex = 0; 

    for(int i = 1 ; i < m.length ; i++) { 
     score = compareMatrices(numFiles.getMatrix(m[i]), imgMatrix); 

     if(score > max) { 
      max = score; 
      maxIndex = i; 
     } 
    } 

    return m[i]; 
} 

最高は

関連する問題