Jump to content

Location of the member of the list.


wimal

Recommended Posts

I need the find out the maximum value and its location (Position number) of a list.

(Eg. ( LIST 1 2 3 4.5 6 1.2)

Need return Max value = 6

Position = 4

Link to comment
Share on other sites

  • Replies 24
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    5

  • pBe

    5

  • Roy_043

    4

  • Grrr

    3

Top Posters In This Topic

(defun foo ( L / i )
 (setq i -1)
 (setq L (mapcar '(lambda (x) (cons x (setq i (1+ i)))) L))
 (car (vl-sort L '(lambda (a b) (apply '> (mapcar 'car (list a b))))))
)

 

(foo '(1 2 3 4.5 6 1.2)) >> (6 . 4)

Link to comment
Share on other sites

Another:

(defun foo ( l / m)
 (cons 
   (setq m (apply 'max l))
   (vl-position m (mapcar 'float l))
 )
)

but result is different:

(foo ( LIST 1 2 3 4.5 6 1.2)) >> ([color="red"]6.0[/color] . 4)

Link to comment
Share on other sites

Maybe this ziele_o2k

 

(defun foo ( l / m)
 (cons 
   (vl-some '(lambda (n )
	(if (setq i (vl-position n l)) n))
     (list (setq m (apply 'max l)) (fix m)))
   i
 )
)

Edited by pBe
Link to comment
Share on other sites

I'd probably take this approach:

(defun foo ( l / i m )
   (setq m (apply 'max l) i -1)
   (vl-some '(lambda ( x ) (setq i (1+ i)) (if (equal x m 1e- (cons x i))) l)
)

 

I don't like to rely on member/vl-position where doubles are involved.

Link to comment
Share on other sites

I'd probably take this approach

 

I don't like to rely on member/vl-position where doubles are involved.

 

Good point, Maybe remove or avoid doubles when building the initial list?

 

(Defun fam ( l / i m n l)
(cons  
(setq  i -1  m (Car (vl-sort l '>)))
(while  (/= m n)
 	    (setq n (car l) i (1+ i) l (Cdr l)) i)
)
)

Edited by pBe
CODE clean up
Link to comment
Share on other sites

Good point, Maybe remove or avoid doubles when building the initial list?

 

By 'doubles' I was referring to double-precision floating-point format data, or AutoLISP reals.

Link to comment
Share on other sites

By 'doubles' I was referring to double-precision floating-point format data, or AutoLISP reals.

 

:lol: retard moment!

Thank you for the clarification LM.

Link to comment
Share on other sites

@Lee:

Instead of the true max value your code can return the 'max-fuzz' value. Why do you consider this to be better?

Link to comment
Share on other sites

@Lee:

Instead of the true max value your code can return the 'max-fuzz' value. Why do you consider this to be better?

 

Roy, I'm not sure I understand what you mean - my code will return the index of the item considered equal to the maximum value to within the tolerance given in the code. I offer this as an alternative to those methods relying upon functions such as member/vl-position to ascertain the position of the maximum value calculated using the max function, which I deem to be unreliable due to the rounding of values stored using double-precision floating-point format.

 

The simplest approach would of course be:

(defun max-idx ( lst / idx rtn )
   (setq rtn (cons (car lst) 0)
         idx 1
   )
   (foreach itm (cdr lst)
       (if (< (car rtn) itm)
           (setq rtn (cons itm idx))
       )
       (setq idx (1+ idx))
   )
   rtn
)

Link to comment
Share on other sites

I don't like to rely on member/vl-position where doubles are involved.
Agreed. Same regarding (max, as a single real in a list of int will make (max return a float. I'd even go from "i don't like to rely" to "it is not reliable".

 

So many ways to skin the cat that it is hard to find a not skinless cat. Poor cats :)

(defun foo (l / p)
  (cons (nth (setq p (last (vl-sort-i l '<))) l) p)
)

meow meow

Link to comment
Share on other sites

@Lee:

What I mean is:

(defun foo ( l / i m ) ; By Lee Mac.
 (setq m (apply 'max l) i -1)
 (vl-some '(lambda ( x ) (setq i (1+ i)) (if (equal x m 1e- (cons x i))) l)
)

(defun PosOfMax (lst / fnd)
 (cons (setq fnd (apply 'max lst)) (vl-position fnd lst))
)

(setq lst (list 0.8 0.9 (- 1.0 1e-9) 1.0))

(foo lst)      => (1.0 . 2)

(PosOfMax lst) => (1.0 . 3)

Note: In BricsCAD the max function does not have the 'int to float' issue Jef! has mentioned.

Link to comment
Share on other sites

OK, so the min and max functions in AutoCAD have this unexpected behavior (as Jef! already mentioned). But for a list containing only doubles or only integers PosOfMax would work just fine. Or is there also some AutoCAD issue with vl-position?

Link to comment
Share on other sites

Just for fun:

 

(defun MaxNumWithPosition ( L / foo )
 
 (defun foo ( i idx num L / tmp fixif b1 b2 )
   (if (and L (setq i (1+ i)) (setq fixif '(( b v )((if b fix '((v)v)) v))))
     (cond 
       ( (not (setq tmp (car L))) (foo i idx num (cdr L)) )
       ( (numberp tmp)
         (setq b1 (eq 'INT (type tmp)))
         (setq tmp (float tmp))
         (if num
           (progn 
             (setq b2 (eq 'INT (type num))) 
             (setq num (float num))
             (if (<= num tmp)
               (foo i (1- i) (fixif b1 tmp) (cdr L))  
               (foo i idx (fixif b2 num) (cdr L))  
             )
           )
           (foo i (1- i) (fixif b1 tmp) (cdr L))  
         )
       )
       ( (foo i idx (fixif b2 num) (cdr L)) )  
     ); cond
     (if num (cons idx num))
   ); if
 )
 
 (foo 0 0 nil L)
)

 

(mapcar 'MaxNumWithPosition
 '(
   (0 1 2 3)
   (0 "a" "b" 1 2 3)
   (0 1 99 2 3)
   ("x" 43.125 0 1 24.1 2 3)
   (a 44.2 b 231.5 c 11.2)
   (88.123123123 42.787878 111.2222315 92.31456)
 )
)
>> ((3 . 3) (5 . 3) (2 . 99) (1 . 43.125) (3 . 231.5) (2 . 111.222))

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...