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

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Error assigning program outputs to a variable

NK_11361416
1-Newbie

Error assigning program outputs to a variable

Mathcad Prime 10
File Attached.

 

When evaluating a range of inputs for the program shown, the outputs are correct. 

 

I need to assign the  outputs to a variable as a column vector to use in subsequent steps.  When I assign the program outputs to a variable, I get error "This value must be a scalar or a vector".  

1. If the output of linterp_2D(A,B,Data,a,0.25) isn't a column vector, what is it?

2. How can I create a column vector (or anything else) from the program output so that I can assign it to a variable?

 

NK_11361416_0-1728063798816.png

 

ACCEPTED SOLUTION

Accepted Solutions
StuartBruff
23-Emerald III
(To:NK_11361416)

The root of your problem is that a is a range variable.  Whenever Mathcad evaluates a range variable, it iterates over the implied values within it range specification and shows the results as it works its way through each implied value.   Unfortunately, the result looks like a vector.  Many new and inexperienced Mathcad users hit this problem. 

 

The simplest way to check if what you're seeing is a vector or a range variable evaluation is to transpose it.  If the result of the evaluation is a vector, the Mathcad will happily transpose the entire vector.  If it's a range variable at work, then a scalar result with raise an error (can't transpose a scalar) or show each individual array result transposed.

 

There are several way around the problem.  Here's one, the happens to use my method of the day - local functions, a very useful tool.  Talking of which, here are some handy range <-> vector conversion functions.

 

2024 10 04 L.png

 

I've renamed your range variable a to arng, then converted it to the vector a.  This allows me to create a local function f that evaluates linterp2D for a single value within a, and then I vectorize over f(a) without the vectorization applying to every array within linterp2D's argument list.

 

2024 10 04 K.png

 

Stuart

View solution in original post

4 REPLIES 4
StuartBruff
23-Emerald III
(To:NK_11361416)

The root of your problem is that a is a range variable.  Whenever Mathcad evaluates a range variable, it iterates over the implied values within it range specification and shows the results as it works its way through each implied value.   Unfortunately, the result looks like a vector.  Many new and inexperienced Mathcad users hit this problem. 

 

The simplest way to check if what you're seeing is a vector or a range variable evaluation is to transpose it.  If the result of the evaluation is a vector, the Mathcad will happily transpose the entire vector.  If it's a range variable at work, then a scalar result with raise an error (can't transpose a scalar) or show each individual array result transposed.

 

There are several way around the problem.  Here's one, the happens to use my method of the day - local functions, a very useful tool.  Talking of which, here are some handy range <-> vector conversion functions.

 

2024 10 04 L.png

 

I've renamed your range variable a to arng, then converted it to the vector a.  This allows me to create a local function f that evaluates linterp2D for a single value within a, and then I vectorize over f(a) without the vectorization applying to every array within linterp2D's argument list.

 

2024 10 04 K.png

 

Stuart

StuartBruff
23-Emerald III
(To:StuartBruff)

A more conventional way of implementing the calculation is to define a range variable to index each element of your intended result vector. 

 

2024 10 04 M.png

 

It's a fairly straightforward method to implement and most people use it.

 

Of course, it implies that you know how big its going to be and don't mind the little bit of mental gymnastics to calculate your 'a' values from the 'i' values.  Difficulties an also arise when reusing worksheets but with different ranges.  Offset errors can crop up and cause heads to be scratched looking for, say, an index with a value less than ORIGIN.  *I* generally find it more convenient to use the previously specified functions to handle range <-> vector conversions.  That way, I don't have to think about the issue too hard.  But, others find the above approach easier to deal with and their data often does not change much in nature or size.

 

Interestingly, there have been a few recent threads where range variables have been at the heart of a user's problems.

 

Stuart

Thank you for the quick and thorough reply, using the local function works perfectly!

StuartBruff
23-Emerald III
(To:NK_11361416)

No worries.

Announcements

Top Tags