Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

RareEarth

Computer
Nov 15, 2010
21
0
0
HU
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.
 
Replies continue below

Recommended for you

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
 
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.
( maybe this wasn't what your last sentence asked for...)

Regards,
Tomas

 
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:

[pre]
@{
$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;
};
};
[/pre]

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)
 
Status
Not open for further replies.
Back
Top