Reputation: 15782
I want to find the minimum and maximum values of this array. At some point it is set to (512, 512)
UShorts. A For loop would be very time consuming for this many points and am looking for something cleaner. SelectMany occurred to me but I don't know how to implement it.
Dim usResult As UShort(,)
edit: I have tried
Dim minValue As UShort = UShort.MaxValue
Dim maxValue As UShort = UShort.MinValue
Dim sw As New Stopwatch()
sw.Start()
For i As Integer = 0 To 511 Step 1
For j As Integer = 0 To 511 Step 1
minValue = Math.Min(usResult(i, j), minValue)
maxValue = Math.Max(usResult(i, j), maxValue)
Next
Next
sw.Stop()
Console.WriteLine(sw.ElapsedMilliseconds)
' This takes 2 to 3 milliseconds
Upvotes: 4
Views: 7502
Reputation: 17875
The simplest way to get min/max of a multi-dimensional array is by doing this:
Dim max As UShort = usResult.Cast(Of UShort).Max()
Dim min As UShort = usResult.Cast(Of UShort).Min()
It doesn't offer better performance than a for loop though. You would need to use a specialized data structure that keeps elements sorted, or keeps track of min/max elements, to get better performance.
Upvotes: 3
Reputation: 15813
A for loop might be much less time-consuming than you expect. Try timing it to see how long it takes to find the min and max 100,000 times with nested loops.
Upvotes: 1