×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# How to get nearest element from List expression NX8.5 and higher

## How to get nearest element from List expression NX8.5 and higher

(OP)
Hi All,
I am trying to find the nearest number element from the given list of numbers. Imagine there are two expression1 and expression2 with data as mentioned below, the outcome I am expecting as expression3
Example:
expression1 = 140; (number type)
expression2 = {20, 25, 30, 40, 50, 80, 100, 125, 150, 200, 300, 500, 750, 1000} ; (list type)
expression3= 150 (number type)
It will be added value, if we can choose direction like floor, round and ceiling, so that I can pick 125, 150 looking forward for kind support.
Solution in NX8.5 to 11 is OK.

### RE: How to get nearest element from List expression NX8.5 and higher

There are brute force ways to do this, I'm not so sure about built in functions to do this directly. Below are some of the functions that can be used. Not very clean but it will find the closest plus/minus.

a=2
list={abs(1-a),abs(2-a),abs(3-a),abs(4-a)}
b=minimum(list)
c=position(b,list)
d=nth(c,{1,2,3,4})

NX 11.0.1.11 Windows 10

### RE: How to get nearest element from List expression NX8.5 and higher

In the Expression editor, there is a button "F(x)" that helps you enter math functions,the correct syntax, it contains a find function, type "floor" an press find, then step by step until you have the finished formula.

Regards,
Tomas

### RE: How to get nearest element from List expression NX8.5 and higher

RareEarth --

Just saw this one... I've been spending a lager fraction of my time on the Siemens NX Design Forum lately.

Here's a brute-force answer (like multicaduser described) that gets you the closest value, with expression1 as the input and expression2 as the list. For your example, you would cut and paste this code into expression3:

@{
$input << expression1;$sorted_list << sort( expression2 , Ascending );
$num << length($sorted_list);
$closest << loop { For$value from 1 to $num by 1; if ($input <= first($sorted_list) ) return first($sorted_list);
if ( $input >= nth($num, $sorted_list) ) return nth($num, $sorted_list); if ( (( nth($value, $sorted_list) <=$input ) &&  ( $input <= nth($value+1, $sorted_list))) && (($input - nth($value,$sorted_list)) < ( nth($value+1,$sorted_list) - $input )) ) return nth($value, $sorted_list); if ( (( nth($value, $sorted_list) <=$input ) &&  ( $input <= nth($value+1, $sorted_list))) && (($input - nth($value,$sorted_list)) >= ( nth($value+1,$sorted_list) - $input )) ) return nth($value+1, $sorted_list); return is 999; }; }; Many of you will recognize this as Knowledge Fusion syntax -- the language underpinning NX Expressions. Basically, this code starts by: a) sorting the list (into$sorted_list), and then
b) getting the length of the list (as $num). It then loops through the values in the list and asks: for each$value in the $sorted_list if the$input is smaller than the first item in the list, return the first item.
if the $input is larger than the last item in the list, return the last item. [and then if neither of the two easy cases are true...] if the$input is:
(between the current value and the next higher value) AND
(the difference between the $input and the current value is LESS THAN the difference between the$input and the next higher value) THEN
return the current value.
if the $input is: (between the current value and the next higher value) AND (the difference between the$input and the current value is GREATER THAN the difference between the \$input and the next higher value) THEN
return the next higher value.
And if after all that you don't find any matches at all, return 999.

...Which should be a signal to design the ranges better.

Hopefully that all makes sense. Enjoy!

Taylor Anderson
NX Product Manager, Knowledge Reuse and NX Design
Product Engineering Software
Siemens Product Lifecycle Management Software Inc.
(Phoenix, Arizona)

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!