cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

How to lookup a value in an array

SFares
12-Amethyst

How to lookup a value in an array

Hello,

i am attaching a mathcad14 sheet. it is not a live sheet becuase i got this shot from a big mathcadsheet. i would like to be able to look up the value that is in column "0" that corresponds to max(I_1,I_2) which are in columns 1 &2, and also do the same thing but seperately for max(I_3,I_4) which are in columns 3 &4. i am expecting the answer for the first one to be 48.707(column 0), and for the second one 48.707(column0). This time it happens to be the same value from column 0, but normaly thaey are different. In case they are more than one value in column 0, that corresponds to single value for max(I_1,I_2), make it to list all values off column 0. similarly for max(I_3,I_4). Thanks for your assistance!!

1 ACCEPTED SOLUTION

Accepted Solutions
Werner_E
24-Ruby V
(To:SFares)

The reason for this effect is because Mathcads various lookup and match routines use the system variable TOL to decide if a value is found. This means it might not be an exact match (thats another difference between Andy's Match and the builtin match).

In your sheet TOL is set to 10^-2 (which is rather low but I guess there might be a reason for this). With this setting lookup will find 10 values in L_1 and L_2 which are around the searched for max within this tolerance and returns the corresponding values in X.Holes. In fact there are only 5 values, but each one is found twice as the columns L_1 and L_2 are identical.

24.10.png

As you had decided you are interested in the first one only, you get it and it is 20.290.

Setting TOL to the default 10^-3 or lower cures the problem and only two matches are found (which is OK as we have the max value in the first and in the second column present (and fortunately in the same row.

So one way to get what you want is to set TOL lower before using lookup and back to the desired value afterwards

24.10.png

or use Andys routine (maybe modified so it return the desired value directly) or use the more versatile routine which I provided at the end of the sheet I posted.

View solution in original post

13 REPLIES 13

It is a question for Werner!

AndyWesterman
4-Participant
(To:SFares)

Easier to test the functions if you supply test data, but I think that this shouold work

Regards

Andy

it is a 10M sheet. i was trying "vlookup" , but it did not work. most likely i am not using it correctly. you can send me how you would look up the value using Mathcad function, and i will try it. Thanks!

Werner_E
24-Ruby V
(To:SFares)

So burn it down to a managable size.

Create a sample worksheet with small vectors and matrices, maybe created by random to demonstrate what you want. This would give us something concrete to work with. You will have to specify what should happen if the max of I_1 & I_2 happens to appear more than once. Lets say the max is 0.943 but it appears in row 14 AND row 16 of I_2 AND in row 17 of I_2. What result do you expect? An error message, the first occurence (row 14 --> 38.373, all3 values in a vector?

While I would prefer a small self written routine one idea is to stack L_1 and L_2 one on top of each other, stack X.Holes on top of itself and use vlookup, match.

Andy,

please see attached file. when i did it as i understood from you, it gave me the row number which is 18, but i want it to show the value 48.707 which is in column 0. what should i change? Thanks!

Fred_Kohlhepp
23-Emerald I
(To:SFares)

match is giving back the correct row value, you just need to point to the right column in Results.

Hi Fred,

i see a zero at the end of what you wrote. is there something wrong there? because it is not gving me an error. Thanks!

Werner_E
24-Ruby V
(To:SFares)

Andy's Match-routine is pretty much the same as the built-in match command.

You could replace

23.10.png

by

23.10.png

Both return the index of the found value. The only difference is when the value is not found. Andy returns -1 and the buitl-in routine would throw an error. But even if you use Andys routine and Freds revision you will get an error if the value is not present in one of the columns (as of -1 is not a valid index).

In any case your problem with this approach is, as I understand it, that your maximum may only be present in one of the two columns or it may be present in both but at different rows. I understood that your goal is to search for the max value in both columns at the same time.

BTW, what on heaven earth is so hard to provide a sheet with dummy data to demonstrate the needs? We already had people just posting pics to be debuged but Mathcad sheets which just contains pics is new to me.

SFares
12-Amethyst
(To:Werner_E)

Werner,

attached is the zipped mathcad file. as you scroll halfway down, i have this area opened. Thanks for your help!

Werner_E
24-Ruby V
(To:SFares)

Too late, I made my own.

And I wouldn't call your sheet a stripped down one which would allow you to demonstrate what you are after and allow us to show how it may be done in a convenient way. I didn't wait for the sheet to finish its calculations but I saw that you already included my suggestions and it looks like it did what you wanted, right?

Anyway here is my sheet attached with an additional way to do it.

SFares
12-Amethyst
(To:Werner_E)

Werner,

Please look at what i did in my sheet. it gave me the value of 20.29, when it should be 48.707. i am not sure what i did wrong. At 20.29, the value for I-1,I_2 is 0.937 which does not match 0.943.

Werner_E
24-Ruby V
(To:SFares)

The reason for this effect is because Mathcads various lookup and match routines use the system variable TOL to decide if a value is found. This means it might not be an exact match (thats another difference between Andy's Match and the builtin match).

In your sheet TOL is set to 10^-2 (which is rather low but I guess there might be a reason for this). With this setting lookup will find 10 values in L_1 and L_2 which are around the searched for max within this tolerance and returns the corresponding values in X.Holes. In fact there are only 5 values, but each one is found twice as the columns L_1 and L_2 are identical.

24.10.png

As you had decided you are interested in the first one only, you get it and it is 20.290.

Setting TOL to the default 10^-3 or lower cures the problem and only two matches are found (which is OK as we have the max value in the first and in the second column present (and fortunately in the same row.

So one way to get what you want is to set TOL lower before using lookup and back to the desired value afterwards

24.10.png

or use Andys routine (maybe modified so it return the desired value directly) or use the more versatile routine which I provided at the end of the sheet I posted.

SFares
12-Amethyst
(To:Werner_E)

Werner,

Thank you so much for your detailed explanation and patience!!

Top Tags