turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Navigation
- NX Design
- Forums
- Blogs
- Knowledge Bases
- Groups

- Siemens PLM Community
- NX Design
- NX Design Forum
- How to get nearest element from List expression

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-06-2018 11:53 AM

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 and higher is OK.

Solved! Go to Solution.

- Tags:
- Expression
- list

9 REPLIES

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-06-2018 02:38 PM - edited 03-06-2018 06:13 PM

Hi @AwesomeAlways,

I understand what you're trying to accomplish, but I don't think it's possible.

Existing ER 6746581 asks for functionality similar to what you're looking for:

"I would like to see a function that would allow me to extract the value from a list that is equal to or greater than an input value. Also a similar function that would extract the value equal to or less than an input value. Both functions should also return the position of the value in the list."

Regards, Ben

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-06-2018 02:59 PM

There is an expression that kind of does that. The issue is that the list of numbers needs to be string values. If will return the correct position from the list but now you have to convert strings to numbers and other things. Here is a screen shot in NX 12 that returns the postion in the list. Sorry I do not have NX 8.5 installed anymore.

Scott

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-06-2018 05:52 PM

@ScottFelber - good find!! And it's available in NX 8.5.

A couple of pointers - the value of ug_findClosestNumberInList returns the array value - so the first value is zero.

Use the Length() function to obtain the quantity of values in the list array. E.g., Length(exp2)

Use MakeNumber to convert the value in the list to a number. E.g., MakeNumber(nth(my_index, exp2))

Expression file attached.

Regards, Ben

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-07-2018 04:33 AM

This solves my requirment.

Thank you Ben and Scott!!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-08-2018 05:33 AM

"+ 1" is missing in the MakeNumber function correct?

MakeNumber(nth(array_index + 1,exp2)

If I use the original code and I type 25 for example, it still shows 20 as the found number, but 25 exists in the list.

With this "+1" I get the correct nearest number. For example input as 22 yields 20 but 23 yields 25.

Take a look at it and tell me what you think.

Cheers

Philip

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-08-2018 10:38 AM

Hi @PhilipCarpenter,

You know I was looking at that, and also playing around with the value for exp1 to see what results MakeNumber(nth(array_index,exp2) would return, and I wasn't completely happy with my floor and ceiling results with and without the "+1". I do agree with your result, I just figured it would be enough to point out that the initial value of the array is going to be zero.

Having spent quite a lot of time trying to accomodate for the results I was getting (with conditional expressions), I decided that I didn't have enough information from the original post to continue, so I put a basic example in my expression file and waited to see how @AwesomeAlways responded to determine whether we needed to discuss the thread further. It looks like Scott's answer was all that was needed.

Regards, Ben

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-09-2018 04:01 AM

The actual requirement is as @BenBroad mentioned in ER 6746581. At present since this function is not available, I will make couple of more checks before passing is further.

For example: I will take both values from

expFloor=minimum ((MakeNumber(nth(array_index + 1,exp2) , (MakeNumber(nth(array_index ,exp2 ))

expCeling=max ((MakeNumber(nth(array_index + 1,exp2) , (MakeNumber(nth(array_index ,exp2 ))

It servers my current requirement but keeping finger cross for ER.

Thanks again for the kind support!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-09-2018 11:02 AM

**test_result** expression gives the ceiling result, **test_result_2** gives the floor result.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-10-2018 05:13 AM

Delighted to see the humble support from the community, till now I was working on self learning but sharing knowledge is great fun!

@cowski1 it is great to see KF function like loop can directly be used in expression (I was using it only in DFA files) Just one addition to your flowchart is adding sort before passing to loop. Yes, the example I gave had sorted data, but there could be chance that users create the list randomly.

In @ScottFelber method that wasn’t the limitation. Thank you @BenBroad for your quick help

Please have look on the modified version.

Follow Siemens PLM Software

© 2018 Siemens Product Lifecycle Management Software Inc