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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Scalar Values from VLOOKUP?

JohnHinman
1-Newbie

Scalar Values from VLOOKUP?

I've used some VLOOKUP functions to extract data, for instance the yield strength of a material if the material name is known. Unfortunately the value returned is an array (a 1 x 1 array, but an array nonetheless). Using this in a logical equation in an "if ... otherwise" program ( if x < y where the y comes from the VLOOKUP) does not seem to work. I get an error message that the computed value must be a scalar.

How do I convert the value to a scalar?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

select the expression that contains the result and apply the matrix subscript operator ('[' from the keyboard or from the matrix toolbar) and type 0 (assuming that you have selected 0-based indexing for matrices).

eg (taken from the lookup Quicksheet - you can get to it from the Help on lookup functions)

model:=vlookup(9, CARS,1)

model = (15)

model[0= 15

or cut out the middleman

model:=vlookup(9,CARS,1)[0

model=15

View solution in original post

13 REPLIES 13

select the expression that contains the result and apply the matrix subscript operator ('[' from the keyboard or from the matrix toolbar) and type 0 (assuming that you have selected 0-based indexing for matrices).

eg (taken from the lookup Quicksheet - you can get to it from the Help on lookup functions)

model:=vlookup(9, CARS,1)

model = (15)

model[0= 15

or cut out the middleman

model:=vlookup(9,CARS,1)[0

model=15

Thank you, Stuart!

MikeArmstrong
5-Regular Member
(To:StuartBruff)

or cut out the middleman

model:=vlookup(9,CARS,1)[0

Correct answer, but why is the [0 required?

I hate having to add the [0.

Mike

By the way, if you are using ORIGIN = 1, you hae to add [1, not [0

MikeArmstrong
5-Regular Member
(To:JohnHinman)

By the way, if you are using ORIGIN = 1, you hae to add [1, not [0

Yes I know because you are selecting the first element of the array.

Mike

Just to elaborate on this. I think the lookup functions should return a scalar, not array if only one variable is found.

Anyone agree?

Mike

Mike Armstrong wrote:

Just to elaborate on this. I think the lookup functions should return a scalar, not array if only one variable is found.

Anyone agree?

Mike

The problem with returning a scalar is that, when multiple values might occur, code would have to be written to accommodate both scalar and vector responses.

I'd prefer it if the lookup functions returned 0 if no values were found(*) or, ideally, an empty matrix (which, unfortunately, Mathcad doesn't implement - it would make life *so* much easier if it did)

Stuart

(*) I do this for a lot of my functions.

MikeArmstrong
5-Regular Member
(To:StuartBruff)

The problem with returning a scalar is that, when multiple values might occur, code would have to be written to accommodate both scalar and vector responses.

Would this really be a problem?

I'd prefer it if the lookup functions returned 0 if no values were found(*) or, ideally, an empty matrix (which, unfortunately, Mathcad doesn't implement - it would make life *so* much easier if it did)

Stuart

(*) I do this for a lot of my functions.

Can't say I've seen that in your functions. I take it you implement that using the on error.

Mike

Mike Armstrong wrote:

The problem with returning a scalar is that, when multiple values might occur, code would have to be written to accommodate both scalar and vector responses.

Would this really be a problem?

Depends upon your definition of 'a problem'. I find it more 'convenient', shall we say, to use a common approach rather than have to write special cases - 'special' often translates into 'bug opportunity' 🙂

I'd prefer it if the lookup functions returned 0 if no values were found(*) or, ideally, an empty matrix (which, unfortunately, Mathcad doesn't implement - it would make life *so* much easier if it did)

Stuart

(*) I do this for a lot of my functions.

Can't say I've seen that in your functions. I take it you implement that using the on error.

I use it, for example, in my standard functions vecsplit and nonzeros. I use on error to trap Mathcad built-ins (in fact, I usually use a purpose built Match function rather than the built-in to avoid having to deal with this error, but otherwise I initialize array results to zero and simply do a scalar check if needed.

Stuart

MikeArmstrong
5-Regular Member
(To:StuartBruff)

I use it, for example, in my standard functions vecsplit and nonzeros. I use on error to trap Mathcad built-ins (in fact, I usually use a purpose built Match function rather than the built-in to avoid having to deal with this error, but otherwise I initialize array results to zero and simply do a scalar check if needed.

Stuart

Can't say I have seen those and I usually I collect all your posted functions

I do tend to avoid the lookup functions now you have mentioned it.

Mike

I agree, not that it matters whether I agree or not.

MikeArmstrong
5-Regular Member
(To:JohnHinman)

I agree, not that it matters whether I agree or not.

It does matter because you are a Mathcad user and contributing to this site.

Mike

How about this?

New lookup function.jpg

Mike

Top Tags