Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X
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
Here is one way, which (also) works in Prime Express:
Example, define a range:
it is:
Apply the function:
Another example:
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:
and call it with:
Success!
Luc
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:
Example:
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:
The range of nonzero positive even numbers below 10 is:
as a vector it is:
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.
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
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 ...
Here's a variant of my utility function vec that seems to work with range variables.
Stuart
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:
Applying range2vec() does not produce a vector:
It is not a(n ordinary) range, because when assigning a range:
I get a range:
anddemonstrates that R is a range.
While
givesandshows that V is neither a vector, nor a range.
Albeit:
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
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.
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.
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