public static MatrixPosition2D UpgmaLowestDistanceIndexes(List <List <decimal> > distanceMatrix) { var lowestValue = decimal.MaxValue; var lowestIndexes = new MatrixPosition2D(); for (var indexX = 0; indexX < distanceMatrix.Count; indexX++) { for (var indexY = 0; indexY < distanceMatrix[indexX].Count; indexY++) { if (indexX == indexY) { continue; } if (indexY < indexX) { continue; } var value = distanceMatrix[indexX][indexY]; if (value < lowestValue) { lowestValue = value; lowestIndexes.X = indexX; lowestIndexes.Y = indexY; } } } return(lowestIndexes); }
/*public static decimal[,] DeleteDistanceMatrixIndex(decimal[,] distanceMatrix, int indexToDelete) * { * var matrix = new decimal[distanceMatrix.GetLength(0) - 1, distanceMatrix.GetLength(1) - 1]; * * for (var x = 0; x < matrix.GetLength(0); x++) * { * for (var y = 0; y < matrix.GetLength(1); y++) * { * matrix[x, y] = distanceMatrix[x >= indexToDelete ? x + 1 : x, y >= indexToDelete ? y + 1 : y]; * } * } * * return matrix; * } * * public static decimal[,] AddDistanceMatrixIndex(decimal[,] distanceMatrix, decimal[] indexToAdd)//, int indexPositionToInsert) * { * var matrix = new decimal[distanceMatrix.GetLength(0) + 1, distanceMatrix.GetLength(1) + 1]; * * for (var x = 0; x < distanceMatrix.GetLength(0); x++) * { * for (var y = 0; y < distanceMatrix.GetLength(1); y++) * { * matrix[x, y] = distanceMatrix[x, y]; * * } * } * * for (var index = 0; index < indexToAdd.Length; index++) * { * matrix[matrix.GetLength(0) - 1, index] = indexToAdd[index]; * matrix[index, matrix.GetLength(1) - 1] = indexToAdd[index]; * } * * return matrix; * } * * public static decimal[] DeleteArrayIndex(decimal[] array, int indexToRemove) * { * var result = new decimal[array.Length - 1]; * * for (var index = 0; index < result.Length; index++) * result[index] = array[index >= indexToRemove ? index + 1 : index]; * * return result; * }*/ public static List <List <decimal> > UpgmaDistanceMatrixNextIteration(List <List <decimal> > distanceMatrix, MatrixPosition2D lowestIndexes) { // find shortest distance, return index of shortest distance, recalc matrix with mean average //var distanceMatrix2 = new decimal[distanceMatrix.GetLength(0), distanceMatrix.GetLength(1)]; //Array.Copy(distanceMatrix, distanceMatrix2, distanceMatrix.Length); var average = new decimal[distanceMatrix.Count]; for (var index = 0; index < distanceMatrix.Count; index++) { var valueX = distanceMatrix[index][lowestIndexes.X]; var valueY = distanceMatrix[index][lowestIndexes.Y]; average[index] = (valueX + valueY) / 2; } var indexToReplace = lowestIndexes.X <= lowestIndexes.Y ? lowestIndexes.X : lowestIndexes.Y; var indexToDelete = lowestIndexes.X > lowestIndexes.Y ? lowestIndexes.X : lowestIndexes.Y; Console.WriteLine(indexToReplace); Console.WriteLine(indexToDelete); for (var index = 0; index < distanceMatrix.Count; index++) { distanceMatrix[index][indexToReplace] = average[index]; distanceMatrix[indexToReplace][index] = average[index]; } if (indexToReplace != indexToDelete) { foreach (var submatrix in distanceMatrix) { submatrix.RemoveAt(indexToDelete); } distanceMatrix.RemoveAt(indexToDelete); } return(distanceMatrix); }