Cancel
Showing results for
Did you mean:
Highlighted

# How to get nearest element from List expression

Creator

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.

9 REPLIES

# Re: How to get nearest element from List expression

Siemens Honored Contributor

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

# Re: How to get nearest element from List expression

Siemens Phenom

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

# Re: How to get nearest element from List expression

Siemens Honored Contributor

@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

# Re: How to get nearest element from List expression

Creator

This solves my requirment.

Thank you Ben and Scott!!

# Re: How to get nearest element from List expression

Valued Contributor
"+ 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

# Re: How to get nearest element from List expression

Siemens Honored Contributor

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

# Re: How to get nearest element from List expression

Creator

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!

# Re: How to get nearest element from List expression

Gears Esteemed Contributor

Here is another way to do it using some KF commands. The test_result expression gives the ceiling result, test_result_2 gives the floor result.

# Re: How to get nearest element from List expression

Creator

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.