因此,我有两个多维数组。
double[][] combinations = new double[10000][3];
double[][] uniqueCombinations = new double[100][3];
数组值示例:
[[1.233, 1.333, 0.76], [1.1, 1.333, 1.333], [0.9, 1.1, 0.9], [1.1, 1.333, 1.333]]
这就是我想要的
[[1.233, 1.333, 0.76], [1.1, 1.333, 1.333], [0.9, 1.1, 0.9]]
我想从组合中获取所有唯一的数组,并用它填充uniqueCombinations。
我尝试过使用此函数,但是它只有5个数组,很奇怪!
public static double[][] removeDuplicate(double[][] matrix) {
double[][] newMatrix = new double[matrix.length][matrix[0].length];
int newMatrixRow = 1;
for (int i = 0; i < matrix[0].length; i++)
newMatrix[0][i] = matrix[0][i];
for (int j = 1; j < matrix.length; j++) {
List<Boolean> list = new ArrayList<>();
for (int i = 0; newMatrix[i][0] != 0; i++) {
boolean same = true;
for (int col = 2; col < matrix[j].length; col++) {
if (newMatrix[i][col] != matrix[j][col]) {
same = false;
break;
}
}
list.add(same);
}
if (!list.contains(true)) {
for (int i = 0; i < matrix[j].length; i++) {
newMatrix[newMatrixRow][i] = matrix[j][i];
}
newMatrixRow++;
}
}
int i;
for(i = 0; newMatrix[i][0] != 0; i++);
double finalMatrix[][] = new double[i][newMatrix[0].length];
for (i = 0; i < finalMatrix.length; i++) {
for (int j = 0; j < finalMatrix[i].length; j++)
finalMatrix[i][j] = newMatrix[i][j];
}
return finalMatrix;
}
答案 0 :(得分:0)
您可以尝试基于哈希表的算法,即为每个矩阵向量计算哈希值,并使用哈希键将向量索引保存在哈希图中。然后根据哈希表索引值构造一个结果矩阵。例如:
// stores height h, side1 a, side2 b
double a, b, h;
//Reads out b from a textbox
private void txbB_TextChanged(object sender, EventArgs e)
{
string parseB = txbB.Text;
b = double.Parse(parseB);
}
//Reads out a from a textbox
private void txbA_TextChanged(object sender, EventArgs e)
{
string parseA = txbA.Text;
a = double.Parse(parseA);
}
//Reads out h from a textbox
private void txbH_TextChanged(object sender, EventArgs e)
{
string parseH = txbH.Text;
h = double.Parse(parseH);
}
//button which calculates the volume of the pyramid
//when clicked and prints it to the label "LblErgebnis"
private void Cmdrechnen_Click(object sender, EventArgs e)
{
double Total = 1/3 * h * a * b;
string Result = Total.ToString();
LblErgebnis.Text = Result;
}
在大型矩阵上仍然可能发生哈希未命中,因此使用了Google Guava提供的MurMur3A代替了 import static org.junit.Assert.assertArrayEquals;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.junit.Test;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
public class ArraysCombination {
private static double[][] COMBINATIONS = {
{1.233, 1.333, 0.76 },
{ 1.1, 1.333, 1.333 },
{ 0.9, 1.1, 0.9 },
{ 1.1, 1.333, 1.333 } };
private static double[][] uniqieCombinations(double[][] all) {
final Map<Integer,Integer> uniqueIdx = new HashMap<>();
// hashing can be replaced with Arrays.hashCode(all[i])
final HashFunction hashFunction = Hashing.murmur3_32(all.length);
for (int i = 0; i < all.length; i++) {
final Hasher hasher = hashFunction.newHasher();
for (int j = 0; j < all[i].length; j++) {
hasher.putDouble(all[i][j]);
}
final Integer hash = hasher.hash().asInt();
if( !uniqueIdx.containsKey(hash) ) {
uniqueIdx.put(hash, Integer.valueOf(i));
}
}
double[][] arr = new double[uniqueIdx.size()][];
Iterator<Integer> it = uniqueIdx.values().iterator();
for (int i=0; i < arr.length; i++ ) {
int idx = it.next();
arr[i] = Arrays.copyOf( all[ idx ], all[idx].length );
}
return arr;
}
@Test
public void shouldFindUniqueCombinations() {
double [][] uniqueCombination = uniqieCombinations(COMBINATIONS);
for (double[] ds : uniqueCombination) {
System.out.println(Arrays.toString(ds));
}
double[][] expected = {{1.233, 1.333, 0.76}, {1.1, 1.333, 1.333}, {0.9, 1.1, 0.9}};
for (int i = 0; i < expected.length; i++) {
assertArrayEquals("Wrong unique combinations", expected[i] , uniqueCombination[i], 0 );
}
}
}