## 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.

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

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

( maybe this wasn't what your last sentence asked for...)

Regards,

Tomas

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

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:

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:

...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)