wimal Posted July 30, 2018 Share Posted July 30, 2018 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 Quote Link to comment Share on other sites More sharing options...
Grrr Posted July 30, 2018 Share Posted July 30, 2018 (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) Quote Link to comment Share on other sites More sharing options...
pBe Posted July 30, 2018 Share Posted July 30, 2018 Another (defun fam ( l / m) (cons (Setq m (car (vl-sort l '>))) (vl-position m l) ) ) Quote Link to comment Share on other sites More sharing options...
ziele_o2k Posted July 30, 2018 Share Posted July 30, 2018 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) Quote Link to comment Share on other sites More sharing options...
pBe Posted July 30, 2018 Share Posted July 30, 2018 (edited) 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 July 30, 2018 by pBe Quote Link to comment Share on other sites More sharing options...
Tharwat Posted July 30, 2018 Share Posted July 30, 2018 Hi, How come 6 is in the fourth position? Are you aware is that the vl-position function returns an integer number with zero based? Quote Link to comment Share on other sites More sharing options...
pBe Posted July 30, 2018 Share Posted July 30, 2018 OP's requirement is as such (Eg. ( LIST 1 2 3 4.5 6 1.2)Need return Max value = 6 Position = 4 Quote Link to comment Share on other sites More sharing options...
Tharwat Posted July 30, 2018 Share Posted July 30, 2018 OP's requirement is as such My reply was forwarded to OP and I know that you know that. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 30, 2018 Share Posted July 30, 2018 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. Quote Link to comment Share on other sites More sharing options...
pBe Posted July 30, 2018 Share Posted July 30, 2018 (edited) 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 July 30, 2018 by pBe CODE clean up Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 30, 2018 Share Posted July 30, 2018 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. Quote Link to comment Share on other sites More sharing options...
pBe Posted July 30, 2018 Share Posted July 30, 2018 By 'doubles' I was referring to double-precision floating-point format data, or AutoLISP reals. retard moment! Thank you for the clarification LM. Quote Link to comment Share on other sites More sharing options...
kpblc Posted July 31, 2018 Share Posted July 31, 2018 Another one: (setq lst '(1 2 3 4.5 6 1.2)) (cons (setq a (apply 'max lst)) (- (length lst) (length (member a lst)))) Quote Link to comment Share on other sites More sharing options...
Roy_043 Posted August 1, 2018 Share Posted August 1, 2018 @Lee: Instead of the true max value your code can return the 'max-fuzz' value. Why do you consider this to be better? Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted August 1, 2018 Share Posted August 1, 2018 @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 ) Quote Link to comment Share on other sites More sharing options...
Jef! Posted August 1, 2018 Share Posted August 1, 2018 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 Quote Link to comment Share on other sites More sharing options...
Roy_043 Posted August 2, 2018 Share Posted August 2, 2018 @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. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted August 2, 2018 Share Posted August 2, 2018 @Roy, as noted in my earlier posts, I avoid methods such as used in your 'PosOfMax' function as I find these unreliable when used with doubles. Quote Link to comment Share on other sites More sharing options...
Roy_043 Posted August 3, 2018 Share Posted August 3, 2018 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? Quote Link to comment Share on other sites More sharing options...
Grrr Posted August 3, 2018 Share Posted August 3, 2018 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)) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.