Jump to content

How can I retrieve the max index and max from a multidimensional array?


Recommended Posts

The 2D-array contains numbers and I want to find the index of the one element that is the highest value of all elements in the matrix.

E.g.,

A 2D matrix with rows

[1,2,5], [4,2,6] [8, 2, 4]

I want to have index (2,0) returned because its value is 8

Edited by wuschelbeutel
Link to comment
Share on other sites

Maybe this :

Global $aMatrix[3][2] = [ _
[1.5, 2.3], _
[2.7, 5.54], _
[1.12, 5.55]]

Global $iMatrixMaxFloatIndex = -1, $iMatrixMaxFloatSubIndex = -1, $iMatrixMaxFloat = 0

For $iIndex = 0 To UBound($aMatrix, 1) -1
For $iSubIndex = 0 To UBound($aMatrix, 2) -1
If $aMatrix[$iIndex][$iSubIndex] > $iMatrixMaxFloat Then
$iMatrixMaxFloat = $aMatrix[$iIndex][$iSubIndex]

$iMatrixMaxFloatIndex = $iIndex
$iMatrixMaxFloatSubIndex = $iSubIndex
EndIf
Next
Next

ConsoleWrite("max float index: " & $iMatrixMaxFloatIndex & ", subindex: " & $iMatrixMaxFloatSubIndex & @CrLf)

Br, FireFox.

Link to comment
Share on other sites

Here a fast hack and not fully tested!

#include <Array.au3>
Global $aInt[3][3] = [[3,2,9], _
                                            [4,2,6], _
                                            [5, 9, 0]]

$iMax = _ArrayMax2($aInt)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iMax = ' & $iMax & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
$aFind = _ArrayFindAll2($aInt, $iMax)
_ArrayDisplay($aFind)

Func _ArrayMax2($array)
    Local $iMax = -1, $i
    Local $iUB = UBound($array, 2)
    If $iUB Then
        For $i = 0 To $iUB - 1
            _ArraySort($array, 1, 0, 0, $i)
            If $iMax < $array[0][$i] Then $iMax = $array[0][$i]
        Next
        Return $iMax
    Else
        _ArraySort($array, 1)
        Return $array[0]
    EndIf
EndFunc

Func _ArrayFindAll2($array, $Val)
    Local $aRes, $x, $y, $i
    Local $iUB = UBound($array)
    Local $iUB2 = UBound($array, 2)
    If $iUB2 Then
        Dim $aRes[$iUB][2]
        For $y = 0 To $iUB - 1
            For $x = 0 To $iUB2 - 1
                If $array[$x][$y] = $Val Then
                    $aRes[$i][0] = $x
                    $aRes[$i][1] = $y
                    $i += 1
                EndIf
            Next
        Next
        If $i Then
            ReDim $aRes[$i][2]
            Return $aRes
        EndIf
        Return SetError(1, 0, 0)
    Else
        $aRes = _ArrayFindAll($array, $Val)
        Return $aRes
    EndIf
EndFunc

It searches for the max value in the array and displays the coordinate where the values have been found (zero based -> 1st row x and 2nd row y coordinate).

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...