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

 

23-Emerald V
September 17, 2024

I think I might be doing some wrong, Luc.  r2v doesn't seem to return a vector when the result is assigned to a variable ...

 

2024 09 17 D.png

 

Here's a variant of my utility function vec that seems to work with range variables.

 

2024 09 17 E.png

 

Stuart

23-Emerald IV
September 18, 2024

I'm afraid you're right. somehow the range2vec() function - and therefore also the r2v() function - , seems to return a strange result. Starting from the range:

LucMeekes_4-1726662748775.png

Applying range2vec() does not produce a vector:

LucMeekes_0-1726662465865.png

It is not a(n ordinary) range, because when assigning a range:

LucMeekes_1-1726662563840.png

I get a range:

LucMeekes_2-1726662587859.pngandLucMeekes_3-1726662619485.pngdemonstrates that R is a range.

While

LucMeekes_5-1726662841035.png

givesLucMeekes_6-1726662856883.pngandLucMeekes_7-1726662870104.pngshows that V is neither a vector, nor a range.

 

Albeit:

LucMeekes_8-1726663055266.png

here it still behaves as a range...

Conclusion: You can't (ab)use fold with ranges.

 

As for the function rangevec(), I have no doubts, since that function uses matrix() which produces arrays.

 

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