Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 18:31 10 May 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Question about MATH(MIN a(), [index%])

Author Message
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 529
Posted: 02:33pm 07 Apr 2025
Copy link to clipboard 
Print this post

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:

  Quote  
MATH(MIN a(), [index%])
Returns the minimum of all values in the a() array, a() can have any number of
dimensions. If the integer variable is specified then it will be updated with the
index of the maximum value in the array. This is only available on one-
dimensional arrays.


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:
  Quote  then it will be updated with the
index of the maximum [typo?] value in the array


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 Kingdom
Posts: 10067
Posted: 02:44pm 07 Apr 2025
Copy link to clipboard 
Print this post

dim index%
dim a%(3)=(4,2,1,6)
? math(min a%(),index%),index%
  Quote  BOUND(array() [,dimension]

This returns the upper limit of the array for the dimension requested.
The dimension defaults to one if not specified. Specifying a dimension value of 0 will return the current value of OPTION BASE.
Unused dimensions will return a value of zero.
For example:
DIM myarray(44,45)
BOUND(myarray(),2) will return 45


Edited 2025-04-08 00:47 by matherp
 
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 529
Posted: 06:01pm 07 Apr 2025
Copy link to clipboard 
Print this post

ha! Cool! Thank you Peter!
 
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 529
Posted: 09:51pm 07 Apr 2025
Copy link to clipboard 
Print this post

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: Netherlands
Posts: 4854
Posted: 06:45am 08 Apr 2025
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 10067
Posted: 07:26am 08 Apr 2025
Copy link to clipboard 
Print this post

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: Germany
Posts: 529
Posted: 07:58am 08 Apr 2025
Copy link to clipboard 
Print this post

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: Germany
Posts: 1526
Posted: 08:37am 08 Apr 2025
Copy link to clipboard 
Print this post

  Amnesie said  ... 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". ...

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
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025