GrantTF Posted July 9, 2017 Posted July 9, 2017 Hi I am trying to get the index for the maximum value from an array, but I can't get it working, see example code: #include <Array.au3> #include <MsgBoxConstants.au3> Local $aArray[1][4] $aArray[0][0] = 8 $aArray[0][1] = 7 $aArray[0][2] = 2 $aArray[0][3] = 4 _ArrayDisplay ($aArray) MsgBox($MB_SYSTEMMODAL, 'Max Index Numeric value is 0:8', _ArrayMaxIndex($aArray, 1) & ":" & _ArrayMax($aArray, 1) ) $aArray[0][3] = 99 _ArrayDisplay ($aArray) MsgBox($MB_SYSTEMMODAL, 'Max Index Numeric value is 3:99', _ArrayMaxIndex($aArray, 1 ) & ":" & _ArrayMax($aArray, 1 ) ) The second pop-up displays 0:8 it does not find the 99 Any help much appreciated
water Posted July 9, 2017 Posted July 9, 2017 How about _ArrayMaxIndex? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Developers Jos Posted July 9, 2017 Developers Posted July 9, 2017 (edited) You are looking for the max value in the columns, not in the rows, so I am not sure that will work like that. Currently it simply returns the first row/column result. This way it works although very costly for large arrays, but merely to demonstrate my point: #include <Array.au3> #include <MsgBoxConstants.au3> Local $aArray[1][4] $aArray[0][0] = 8 $aArray[0][1] = 7 $aArray[0][2] = 2 $aArray[0][3] = 4 _ArrayTranspose ( $aArray ) _ArrayDisplay ($aArray) MsgBox($MB_SYSTEMMODAL, 'Max Index Numeric value is 0:8', _ArrayMaxIndex($aArray, 1) & ":" & _ArrayMax($aArray, 1) ) _ArrayTranspose ( $aArray ) $aArray[0][3] = 99 _ArrayTranspose ( $aArray ) _ArrayDisplay ($aArray) MsgBox($MB_SYSTEMMODAL, 'Max Index Numeric value is 3:99', _ArrayMaxIndex($aArray, 1 ) & ":" & _ArrayMax($aArray, 1 ) ) _ArrayTranspose ( $aArray ) Jos Edited July 9, 2017 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
GrantTF Posted July 9, 2017 Author Posted July 9, 2017 Hi Jos Thanks for the swift reply your solution works fine, Is there no way to get the maximum value from rows in an Array? many thanks Grant
mikell Posted July 9, 2017 Posted July 9, 2017 (edited) It's quite easy to build a custom func Local $aArray[1][4] $aArray[0][0] = 8 $aArray[0][1] = 7 $aArray[0][2] = 2 $aArray[0][3] = 4 Msgbox(0,"", _test($aArray)) $aArray[0][3] = 99 Msgbox(0,"", _test($aArray)) Func _test($aArray) Local $nmax = Number($aArray[0][0]), $imax = 0 For $i = 1 to UBound($aArray, 2)-1 If Number($aArray[0][$i]) > $nmax Then $nmax = $aArray[0][$i] $imax = $i EndIf Next Return $imax & ":" & $aArray[0][$imax] EndFunc Edit I didn't see the last post. In this sample _test func you could use a parameter for the row number, or loop through rows etc Edited July 9, 2017 by mikell
GrantTF Posted July 9, 2017 Author Posted July 9, 2017 Thanks Mikell I thought as there was a function in Array.au3 to get Max Column, there would be one for max in row as well Thanks for your help Grant
RTFC Posted July 9, 2017 Posted July 9, 2017 Whacking a mosquito with Thor's hammer. This is total overkill for such a tiny array, of course, but if you need to find min/max in much larger numeric arrays and obtain fast results, Eigen is your friend. #include <Array.au3> #include <MsgBoxConstants.au3> #include ".\Eigen4AutoIt.au3" ; you would need to install this library of mine _Eigen_Startup() Local $aArray[1][4] $aArray[0][0] = 8 $aArray[0][1] = 7 $aArray[0][2] = 2 $aArray[0][3] = 4 $matA=_Eigen_CreateMatrix_FromArray($aArray) _MatrixDisplay ($matA) ; example 1: retrieving all specs in one array in one go $specs=_Eigen_MatrixSpecs($matA) _ArrayDisplay($specs,"specs") MsgBox($MB_SYSTEMMODAL, 'Max Index Numeric value is 0:8', "Max value: " & $specs[11][1] & " found at row " & $specs[14][1] & ", col " & $specs[15][1] ) _Eigen_WriteMatrixValue($matA,0,3,99) _MatrixDisplay ($matA) ; example 2: retrieving individual specs as single values MsgBox($MB_SYSTEMMODAL, 'Max Index Numeric value is 3:99', "Max value: " & _Eigen_MatrixSpecs_Single($matA,11) & " found at row " & _Eigen_MatrixSpecs_Single($matA,14) & ", col " & _Eigen_MatrixSpecs_Single($matA,15) ) _Eigen_Cleanup() My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O
UEZ Posted July 9, 2017 Posted July 9, 2017 Here the next step code -> nth number expandcollapse popup#include <Array.au3> $numbers = "40,3,-2.5,-10,1000,-20202.188,+50,-1.937462,3.14" MsgBox(0, "Test", $numbers & @CRLF & @CRLF & _ "5th smallest number: " & nNumber($numbers, 5, 0) & @CRLF & _ "3rd biggest number: " & nNumber($numbers, 3, 1)) ; #FUNCTION# ===================================================================================================================== ; Name...........: nthNumber ; Description ...: Returns the nth biggest or smallest number ; Syntax.........: nNumber($numbers, $n, $dir = 0) ; Parameters ....: $numbers - the numbers either from a string or from an array ; $n - search for n biggest or smallest number where n is an integer ; $dir - 0 search for smallest number, 1 for biggest number ; Return values .: Success - Returns the appropriate number and set @error to 0 ; Failure - Returns error code: ; 1 - Wrong delimiter in string! Valid is comma (,) only! ; 2 - No numbers found! ; 3 - n is not an integer ; 4 - n is not in boundary of amount of numbers ; Remarks .......: Needs the function CompSort() for sorting the internal array ; Author ........: UEZ ; Version .......: 0.90 Build 2010-11-11 Beta ; ================================================================================================================================ Func nNumber($numbers, $n, $dir = 0) If IsArray($numbers) Then $num = _ArrayToString($numbers, ",") If @error Then Return SetError(1, 0, "Error! Wrong delimiter in string! Valide is comma (,) only!") $numbers = $num EndIf Local $aNum = StringRegExp($numbers, "(-\d+\.\d+|\d+\.\d+|\d+)", 3) If Not IsArray($aNum) Then Return SetError(2, 0, "Error! No numbers found!") If Not IsInt($n) Then Return SetError(3, 0, "Error! n is not an integer") If $n < 1 Or $n > UBound($aNum) - 1 Then Return SetError(4, 0, "Error! n is not in boundary of amount of numbers") CompSort($aNum) If Not $dir Then Return $aNum[$n - 1] ;n smallest number Return $aNum[UBound($aNum) - $n] ;n biggest number EndFunc Func CompSort(ByRef $aArray) Local $gap = UBound($aArray) Local $swaps, $i, $tmp Do $gap = Int($gap / 1.247330950103979) If $gap < 1 Then $gap = 1 $i = 0 $swaps = 0 Do If Number($aArray[$i]) > Number($aArray[$i + $gap]) Then $tmp= Number($aArray[$i]) $aArray[$i] = Number($aArray[$i + $gap]) $aArray[$i + $gap] = $tmp $swaps = 1 EndIf $i += 1 Until $i + $gap > UBound($aArray) - 1 Until $gap = 1 And $swaps = 0 EndFunc 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now