Skip to main content
3-Newcomer
September 9, 2024
Question

Converting range variables to vectors.

  • September 9, 2024
  • 3 replies
  • 4386 views

Whenever I define a range variable and then try to do some matrix operations on it (like finding its length, max/min and transpose...) Mathcad responds with an error saying the operation can all be done on a vector or array. How do I convert my range variable into an array?

Version: Mathcad Prime 10

3 replies

23-Emerald IV
September 9, 2024

Here is one way, which (also) works in Prime Express:

LucMeekes_0-1725915297326.png

Example, define a range:

LucMeekes_1-1725915321634.png

it is:

LucMeekes_2-1725915337353.png

Apply the function:

LucMeekes_3-1725915350941.png

Another example:

LucMeekes_4-1725915368839.png

LucMeekes_5-1725915378284.png

Note that you have to supply the second parameter to the function, which can be anything.

After the definition of range2vec() you can define a shorthand function:

LucMeekes_6-1725915634422.png

and call it with:

LucMeekes_7-1725915653557.png

 

Success!
Luc

 

21-Topaz II
September 9, 2024

Hi,

Another simple way is:

Capture.JPG

Capture2.JPG

23-Emerald IV
September 9, 2024

Nice, but:

Your simple way only works if all values in the range qualify to be an index to a vector. That is not the case  when values in the range are below ORIGIN. It is also not the case when values in the range are not integer. Further you get undesired 0-values in the resulting array when the range does not start with the value of ORIGIN, and when the range skips intermediate integers.

An alternative for unifspace, which also works for Prime Express is:

LucMeekes_0-1725918691185.png

Example:

LucMeekes_1-1725918735736.png

As I understood, the OP requested a function to convert a(n existing) range into a vector. If you want to create a vector from the same data used to create a range you might use:

LucMeekes_2-1725919064639.png

The range of nonzero positive even numbers below 10 is:

LucMeekes_3-1725919124045.png

as a vector it is:

LucMeekes_4-1725919143563.png

Success!
Luc

 

23-Emerald V
September 10, 2024

If you use the full version of Mathcad Prime 10, here's a function, vec, that you can use to convert a range variable to a vector.  vec will also convert a matrix or nested array to a vector.

 

2024 09 10 A.png

 

A nice feature of vec is that it doesn't require data to be equally spaced (except in the case of range variables).

 

The clunky initialization and subsequent submatrix to remove the zero are necessitated by the lack of any empty matrix in Mathcad Prime. 

 

Earlier versions of Mathcad and, IIRC, Mathcad Prime allowed vec to directly convert a range operator to a vector, but the ability to directly input a range operator as a parameter was lost somewhere along the line.

 

Here's a simpler version that only flattens an array to one level but offers the virtue of being much easier to remember how to implement.

 

2024 09 10 B.png

 

I've got a slightly more sophisticated version lying around somewhere that works (worked) in Mathcad Prime Express.  I'll see if I can find it.  It might be of use/interest to somebody.

 

Stuart

 

I've just realized that it is approximately the 25th anniversary of my first request for an empty matrix and 20th anniversary for my first request for true multi-dimensional arrays, features that have long been included in Mathcad's main competitors. Happy Birthday, feature requests! 🙂

Community Manager
September 17, 2024

Hi @NM_11073026,

 

I wanted to see if you got the help you needed.


If so, please mark the appropriate reply as the Accepted Solution. It will help other members who may have the same question.
Please note that industry experts also review the replies and may eventually accept one of them as solution on your behalf.
Of course, if you have more to share on your issue, please pursue the conversation. 

 

Thanks,
Anurag