How to find the minimum and maximum value of a multidimensional array?

I want to find the minimum and maximum values ​​of this array. At one point, it is set to (512, 512) UShorts. And for the cycle there would be a lot of time for this, a lot of points, and I'm looking for something cleaner. SelectMany happened to me, but I don’t know how to implement it.

 Dim usResult As UShort(,) 

edit: I 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 
+4
source share
2 answers

And for a cycle, it can be much less time than you expect. Try counting the time to find out how long it takes to find the minimum and maximum 100,000 times with nested loops.

+1
source

The easiest way to get the min / max of a multidimensional array is to do:

 Dim max As UShort = usResult.Cast(Of UShort).Max() Dim min As UShort = usResult.Cast(Of UShort).Min() 

However, it does not provide better performance than the for loop. You will need to use a specialized data structure that keeps items sorted or keeps track of min / max elements to get the best performance.

+3
source

All Articles