Sign in to follow this  
Followers 0
wuschelbeutel

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

8 posts in this topic

#1 ·  Posted (edited)

MsgBox(0, 'Max Index String value', _ArrayMaxIndex($Matr))

MsgBox(0, 'Max String value', _ArrayMax($Matr))

don't work.

Edited by wuschelbeutel

Share this post


Link to post
Share on other sites



Hi,

Take a look at the function UBound.

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

I am familiar with this function but I dont know how it can help me get the max.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I am familiar with this function but I dont know how it can help me get the max.

What do you mean by the max, the max array index with no empty value ?

Br, FireFox.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
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.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

thanks

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
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
Sign in to follow this  
Followers 0