2017-03-20 17 views
0

Math.random()とシンプレックスノイズを使って0と1の間で2つの乱数リストを生成しました。ピークを持つようにランダムリストをソートしようとしていますシンプレックスノイズリストと同じ場所にトラフがあります。別のリストに基づいてリストを整理する

これはこれまで私が行ってきたことです。

public float[] organize(float[] list) { 
    float[] organizedList = new float[list.length]; 

    for (int i = 0; i < simplexList.length; i++) { 
     float smallestDifference = 2; 

     for (int j = 0; j < list.length; j++) { 
      float difference = list[j] - simplexList[i]; 

      if (difference < smallestDifference) { 
       smallestDifference = difference; 

       organizedList[i] = list[j]; 
      } 
     } 
    } 

    return organizedList; 
} 

このメソッドは、単純配列内の1に最も近いランダムな配列内のアイテムを見つけ、組織し、リストにそれを追加することになっています。私はこれが良い方法か、よりよい解決策があるかどうか疑問に思っていましたか?

+0

'Math.random()'を使って生成されたデータを含む配列をソートし、最も近いものをバイナリ検索すると、アルゴリズムの計算量がO(n^2)からO * log(n))。 – Palle

+1

あなたは 'Math.abs(list [j] - simplexList [i])'ではないと確信していますか? –

答えて

0

ランダムリストのK番目に大きいアイテムを使用して、シンプレックスリストのK番目に大きいアイテムを置き換えます。これは、少なくともピークとトラフの配置が進む限り、結果の形状が元の形状と一致することを保証するはずです。

はそれを行うには、私がしたい:

  • ソートシンプレックスリストを、
  • ソートにそのリスト内のランダムなリスト
  • 結果リストを作成し、各項目の元のインデックスのトラックを維持しながら、ソートされたランダムリストから各アイテムを順番に取り出し、ソートされたシンプレックスリスト内の対応するアイテムの元のインデックスの結果リスト内に配置することによって行われる。

厄介な部分は、ソートされたシンプレックス値の元のインデックスを含むリスト。元のシンプレックス値とそのインデックスを含む各項目を使って拡張リストを作成し、そのリストをシンプレックス値でソートすることが簡単な方法です。

もう1つのやり方では、インデックスを昇順[0,1,2,3、...]で含むリストから開始し、次にリストによってリストされるjava.util.Collections.sort compare()メソッドが元のsimplex_listからの適切な値を比較したjava.util.Comparatorを適用します。

simplex_list = [ 8, 10, 3, 2, 6, 20 ] 

# Pair each simplex value with its index 
augmented_simplex_list = [ [email protected], [email protected], [email protected], [email protected], [email protected], [email protected] ] 

# Sort the [email protected] items according to value 
sorted_augmented_simplex_list = [ [email protected], [email protected], [email protected], [email protected], [email protected], [email protected] ] 

# Discard the value part of each item, leaving only the original 
# list index 
# 
sorted_simplex_index_list = [ 3, 2, 4, 0, 1, 5 ] 

# Sort the random list 
random_list = [ 5, 7, 1, 8, 9, 4 ] 
sorted_random_list = [ 1, 4, 5, 7, 8, 9 ] 

# Take each item of sorted_random_list and place it at the index 
# in the result list indicated by the corresponding item in 
# the sorted_simplex_index_list. That is, 
# sorted_random_list[0] goes to result_list[sorted_simplex_index_list[0]], 
# sorted_random_list[1] goes to result_list[sorted_simplex_index_list[1]], 
# and so on, giving: 
# 
result_list = [ 7, 8, 4, 1, 5, 9 ] 

元シンプレックスリストの形状をしている:アプローチ「インデックスで増補シンプレックス値」を使用して、例えば

public in compare(Integer indexA, Integer indexB) { 
    return simplex_list.get(indexA).compareTo(simplex_list.get(indexB)); 
} 

:それは次のようになります。

もちろん、拡張されたシンプレックスリストアプローチを使用すると、明示的にsorted_simple_index_listを作成することはできませんが、sorted_augmented_simplex_listからアイテムのインデックス部分を取得するだけです。

関連する問題