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

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

Converting range variables to vectors.

NM_11073026
3-Newcomer

Converting range variables to vectors.

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

8 REPLIES 8
LucMeekes
23-Emerald III
(To:NM_11073026)

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

 

Hi,

Another simple way is:

Capture.JPG

Capture2.JPG

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

 

I became so annoyed with nested vectors taking up so much vertical real estate that I wrote an Express-friendly utility function, T, to handle the situation.

 

2024 09 17 F.png

 

Naturally, it would be much simpler written as a program.  However, I did my best Bob the Builder impression and reused the auxiliary functions elsewhere in my Express Library.

 

Stuart 

 

StuartBruff
23-Emerald III
(To:LucMeekes)

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

LucMeekes
23-Emerald III
(To:StuartBruff)

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

 

 

StuartBruff
23-Emerald III
(To:NM_11073026)

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! 🙂

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 

Announcements

Top Tags