I'm trying to find the lowest density in a multi-dimensional array.
Say a array looks like this
1 1 1
1 1 <- Lowest density is this one row:1 column:2.
1 1 1
1 1
1 1 1
1 <- Lowest density is this one row 2: column:1.
Here is my broken code
Public ProcessedBits()() As Byte
'Lowest Density detector to find out which values have the highest priority.
Dim nextLowestDensityColumn As Integer = Integer.MaxValue
Dim nextLowestDensityRow As Integer = Integer.MaxValue
For Row = 0 To Uniques.Length - 1
For Column = 0 To Uniques(Row).Length - 1 'check each column in current row.
If ProcessedBits(Row)(Column) = 1 Then 'if the column is processed, then skip it and process the next one.
If Column < nextLowestDensityColumn AndAlso Row < nextLowestDensityRow Then
nextLowestDensityColumn = Column 1
nextLowestDensityRow = Row
End If
End If
Next
Next
If nextLowestDensityRow <> Integer.MaxValue AndAlso nextLowestDensityColumn <> Integer.MaxValue Then
Row = nextLowestDensityRow
End If
Semi fixed code
'Lowest Density detector to find out which values have the highest priority.
Dim nextLowestDensityColumn As Integer = Integer.MaxValue
Dim nextLowestDensityRow As Integer = Integer.MaxValue
For Row = 0 To Uniques.Length - 1
For Column = 0 To Uniques(Row).Length - 1 'check each column in current row.
If ProcessedBits(Row)(Column) = 1 AndAlso Uniques(Row).Length <> Column 1 AndAlso ProcessedBits(Row)(Column 1) = 0 Then 'if the column is processed, then skip it and process the next one.
If (Column 1) < nextLowestDensityColumn Then
nextLowestDensityColumn = Column 1
nextLowestDensityRow = Row
End If
End If
Next
Next
If nextLowestDensityRow <> Integer.MaxValue AndAlso nextLowestDensityColumn <> Integer.MaxValue Then
Row = nextLowestDensityRow
End If
CodePudding user response:
Fixed it
'Lowest Density detector to find out which values have the highest priority.
Dim nextLowestDensityColumn As Integer = Integer.MaxValue
Dim nextLowestDensityRow As Integer = Integer.MaxValue
For Row = 0 To Uniques.Length - 1
For Column = 0 To Uniques(Row).Length - 1 'check each column in current row.
If ProcessedBits(Row)(Column) = 1 AndAlso Uniques(Row).Length <> Column 1 AndAlso ProcessedBits(Row)(Column 1) = 0 Then 'if the column is processed, then skip it and process the next one.
If (Column 1) < nextLowestDensityColumn Then
nextLowestDensityColumn = Column 1
nextLowestDensityRow = Row
End If
End If
Next
Next
If nextLowestDensityRow <> Integer.MaxValue AndAlso nextLowestDensityColumn <> Integer.MaxValue Then
Row = nextLowestDensityRow
End If