Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X
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!
Solved! Go to Solution.
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
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!
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
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.
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
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.
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?
Mike