|
List Info
Thread: max item in a list
|
|
| max item in a list |
  United States |
2008-06-14 09:44:17 |
|
Hi, Can anyone help me in identifying the maximum item in a list please.
In my program patches have a list on which turtles follow a specific item.
I thought i could write;
let max-chem (item max chemical)
and then just call the 'max-chem' but i get an error saying "ITEM expected 2 inputs, a number and a string or list".
Thanks for any help...........I've been working on this programme using lists for a while now - when i finally finish will be happy to share it if of interest.
Cheers, tim
__._,_.___
.
__,_._,___
|
| Re: max item in a list |
  United States |
2008-06-14 21:40:15 |
|
If chemical is a list, then "max chemical" is the value of the maximum
item in the list.
If you don't want the value, but need the index you have to go around
the house a bit...by getting the value ias above, then using that
value to find the position.
e.g.:
let chemical [ 4 3 2 1 0 6 9 ]
let chem-max-value max chemical
;; chem-max-value contains the maximum value
let chem-max-index position chem-max chemical
;; chem-max-index contains the index of the max value
;; (but you already have the value, so what do you need the index for?)
~~James
On Sat, Jun 14, 2008 at 10:44 AM, chunkyorkshireman
< chunkyorkshireman%40yahoo.co.uk">chunkyorkshireman yahoo.co.uk> wrote:
> Hi,
>
> Can anyone help me in identifying the maximum item in a list please.
>
> In my program patches have a list on which turtles follow a specific item.
>
> I thought i could write;
>
> let max-chem (item max chemical)
>
> and then just call the 'max-chem' but i get an error saying "ITEM expected 2
> inputs, a number and a string or list".
>
> Thanks for any help...........I've been working on this programme using
> lists for a while now - when i finally finish will be happy to share it if
> of interest.
>
> Cheers, tim
__._,_.___
.
__,_._,___
|
| Re: max item in a list |
  United States |
2008-06-15 06:30:13 |
|
On Jun 14, 2008, at 10:44 AM, chunkyorkshireman wrote: Can anyone help me in identifying the maximum item in a list please. In my program patches have a list on which turtles follow a specific item. I thought i could write; let max-chem (item max chemical) and then just call the 'max-chem' but i get an error saying "ITEM expected 2 inputs, a number and a string or list". Thanks for any help...........I've been working on this programme using lists for a while now - when i finally finish will be happy to share it if of interest. Cheers, tim
Tim, If I understand correctly, you want to find the item number of the maximum value in a list. Here is one way:
to-report max-item [ #list ] ; item number of maximum value in given list let $max-value first #list let $max-item 0 let $item 1 foreach but-first #list [ if ? > $max-value [ set $max-value ? set $max-item $item ] set $item $item + 1 ] report $max-item end
HTH, Jim LYons
__._,_.___
.
__,_._,___
|
| Re: max item in a list |
  United States |
2008-06-15 07:03:49 |
|
Tim, After only a little more coffee, this one-liner version occurred to me:
to-report max-item2 [ #list ] report position (max #list) #list end
Testing shows this to be generally faster than the long way.
Cheers, Jim
On Jun 14, 2008, at 10:44 AM, chunkyorkshireman wrote: Can anyone help me in identifying the maximum item in a list please. In my program patches have a list on which turtles follow a specific item. I thought i could write; let max-chem (item max chemical) and then just call the 'max-chem' but i get an error saying "ITEM expected 2 inputs, a number and a string or list". Thanks for any help...........I've been working on this programme using lists for a while now - when i finally finish will be happy to share it if of interest. Cheers, tim
__._,_.___
.
__,_._,___
|
| Re: max item in a list |
  United States |
2008-06-15 07:32:30 |
|
Does let max-chem max chemical
work?
Danny
--- On Sat, 6/14/08, chunkyorkshireman <chunkyorkshireman yahoo.co.uk> wrote:
From: chunkyorkshireman <chunkyorkshireman yahoo.co.uk> Subject: [netlogo-users] max item in a list To: netlogo-users yahoogroups.com Date: Saturday, June 14, 2008, 3:44 PM
Hi, Can anyone help me in identifying the maximum item in a list please.
In my program patches have a list on which turtles follow a specific item.
I thought i could write;
let max-chem (item max chemical)
and then just call the 'max-chem' but i get an error saying "ITEM expected 2 inputs, a number and a string or list".
Thanks for any help........ ...I've been working on this programme using lists for a while now - when i finally finish will be happy to share it if of interest.
Cheers, tim
|
__._,_.___
.
__,_._,___
|
| Re: max item in a list |
  United States |
2008-06-16 01:37:33 |
|
MAX works to give the maximum VALUE from the list. And if that's what
you need, you are done. To get the INDEX of the maximum value requires
a tiny bit of extra efffort, but very tiny, as Jim's terrific
one-liner shows.
~~James
On Sun, Jun 15, 2008 at 8:32 AM, DJ < gameswarrior%40yahoo.com">gameswarrior yahoo.com> wrote:
> Does
> let max-chem max chemical
>
> work?
>
> Danny
>
>
> --- On Sat, 6/14/08, chunkyorkshireman < chunkyorkshireman%40yahoo.co.uk">chunkyorkshireman yahoo.co.uk>
> wrote:
>
> From: chunkyorkshireman < chunkyorkshireman%40yahoo.co.uk">chunkyorkshireman yahoo.co.uk>
> Subject: [netlogo-users] max item in a list
> To: netlogo-users%40yahoogroups.com">netlogo-users yahoogroups.com
> Date: Saturday, June 14, 2008, 3:44 PM
>
> Hi,
>
> Can anyone help me in identifying the maximum item in a list please.
>
> In my program patches have a list on which turtles follow a specific item.
>
> I thought i could write;
>
> let max-chem (item max chemical)
>
> and then just call the 'max-chem' but i get an error saying "ITEM expected 2
> inputs, a number and a string or list".
>
> Thanks for any help........ ...I've been working on this programme using
> lists for a while now - when i finally finish will be happy to share it if
> of interest.
>
> Cheers, tim
__._,_.___
.
__,_._,___
|
[1-6]
|
|