![]() |
Forum Index : Microcontroller and PC projects : Question about MATH(MIN a(), [index%])
Author | Message | ||||
Amnesie Guru ![]() Joined: 30/06/2020 Location: GermanyPosts: 529 |
Hello, I am trying to get the minimum value of an one dimensional array but I need to ignore the first array value. The manual says: First of all, there is a copy & paste typo, I think it should read "minimum value" not "maximum value". But I have problems to understand what this text means. Can I accomplish this with the [index%] - parameter? The manual states: What is "it"? The variable? .. I just don't get it... ![]() Or are there other (simple & efficent) ways to accomplish this? I just need to skip the first array value and then get the minimum of the array. Another idea would be to copy the original array into another array and skip the first value and then do MATH(MIN a())... But since the array is growing, something like length = len(array()) would be helpful. But I can't find anything in the manual which gives me the length of an one dimensional array... Greetings Daniel Edited 2025-04-08 00:40 by Amnesie |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10067 |
dim index% dim a%(3)=(4,2,1,6) ? math(min a%(),index%),index% Edited 2025-04-08 00:47 by matherp |
||||
Amnesie Guru ![]() Joined: 30/06/2020 Location: GermanyPosts: 529 |
![]() |
||||
Amnesie Guru ![]() Joined: 30/06/2020 Location: GermanyPosts: 529 |
Hello, thanks to your help and hint with the Bound(a%()) I was able to solve my problem and wrote a little "program" (just to make things more clear for me, maybe one day it is useful for others if they use the forum search). 'Example Program for Math(Min a%(),index%)) and Bound(a%) ' 'Problem: Imagine you need to get the MINIMUM from an ARRAY but you must 'ignore the first (zero) element from that array. CLS Print "Problem: Imagine you need to get the MINIMUM from an ARRAY but you must" Print "ignore the first (zero) element from that array." Print "" Dim index% Dim a%(4)=(7,3,10,2,8) 'original Array Dim c%(Bound(a%())-1) Print "Array: a(4)=(7,3,10,2,8)" Print "------------------------" Print "Minimum in Array: "+Str$(Math(min a%(),index%)) Print "Position in Array: "+Str$(index%) Print "------------------------" Print "Upper limit ('length') of Array: "+Str$(Bound(a%())) Print "" 'Print every value from Array Print "a%() elements: " For n=0 To Bound(a%()) Print a%(n); Next n Print "" Print "" 'Print all element values except the zero element and copy over to array c%() Print "a%() elements, printed without element zero (value 7): " For n=1 To Bound(a%()) Print a%(n); c%(n-1) = a%(n) 'copy over Array values for each element Next n Print "" Print "" Print "new c%() array (copy from a%() but without element zero): " For n=0 To Bound(c%()) Print c%(n); Next n Print "" Print "" Print "Final step is getting the minimum from the new c%() Array:" Print "" Print "Array: c(3)=(3,10,2,8)" Print "------------------------" Print "Minimum in Array: "+Str$(Math(min c%(),index%)) Print "Position in Array: "+Str$(index%) Print "------------------------" Print "Upper limit ('length') of Array: "+Str$(Bound(c%())) Print "" Again thanks for help! Greetings Daniel Edited 2025-04-08 07:52 by Amnesie |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 4854 |
Daniel, I used a different method. It uses a dummy variable. It is much faster since you avoid a loop in MMBasic for all but 1 element of the array. This becomes slow when the array size is large (as in my logic analyzer, where the array is 4096 in size). dummy%=a%(0) 'just to keep the value if you need it later a%(0)=math(mean a%()) 'replace a%(0) with a value between min and max minimum = math(min a%(),index%) 'do the desired math a%(0)=dummy% 'restore the old a%(0) value in case you need it Volhout Edited 2025-04-08 16:49 by Volhout PicomiteVGA PETSCII ROBOTS |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10067 |
Same idea but faster dummy%=a%(0) a%(0)=&H7FFFFFFFFFFFFFFF 'biggest positive number minimum%=math(min a%()) a%(0)=dummy% |
||||
Amnesie Guru ![]() Joined: 30/06/2020 Location: GermanyPosts: 529 |
Volhout, I had to look for one or two minutes at your code, to really understand what you were doing. This is pure genius and elegant! Just wow... ![]() ![]() So simple and it does exactly what I want. But I am glad Peter first showed me that there is a Bound(a%()) function and how to use the index%. I always searched the manual for "length of array" .. didn't know it could be "Bound". Peter, impressive how much one can optimize the code! The only downside for ME (!) is I have to comment the code extensively to make sure I still understand it in a few years ![]() Thank you both! Greetings Daniel Edited 2025-04-08 18:00 by Amnesie |
||||
twofingers Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1526 |
Don't lie! ![]() ![]() I gave you an example a few days ago with "Bound"! ![]() https://www.thebackshed.com/forum/ViewTopic.php?FID=16&TID=17821&LastEntry=Y#236744#236734 On error skip' If Bound(C(),1)<>15 Then Dim integer c(15)=(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) Colour Map c(),c() EndIf I used it in a different context. Have fun learning! ![]() Regards Michael ... I'm just kidding! ![]() Edited 2025-04-08 18:54 by twofingers causality ≠ correlation ≠ coincidence |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |