Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X
Hi there
I want to exclude some values from d when it is bigger than the width .... I want the program to just exclude the values that do not fulfil the condition, I used this program ,but it shows strange error ...I'm sure both of them have the same units
How to fix this ?
Solved! Go to Solution.
You may also use a generic utility function to do the job:
You may then use either
or in one go
You have to decide what to return in case all elements have to be deleted. As Prime does not provide an empty array you may chose 0 or NaN or, like I did, a 1x1 array containing NaN (so I can still use the transpose operator even in that case).
Here some examples of usage
Delete the word "rows", just use if d[i < width
A simpler way probably would be to just use
or
Caution: This will fail, if ALL values in d are smaller than width or if ALL are larger or equal.
EDIT: See a more generic function which also deals with these special cases in my answer below.
You may also use a generic utility function to do the job:
You may then use either
or in one go
You have to decide what to return in case all elements have to be deleted. As Prime does not provide an empty array you may chose 0 or NaN or, like I did, a 1x1 array containing NaN (so I can still use the transpose operator even in that case).
Here some examples of usage
Neat.
My preference is to return 0, as that way, it is clear that the result is not a vector and can be conveniently tested using a boolean. In addition, there are cases where [NaN] might be a valid (vector) result.
To deal with the transpose problem, I use a nifty transpose function, usually defined as T (in Constant Math Style), which transposes array inputs but leaves scalars alone.
I sometimes also use it (or suitable variants) under vectorization. I have other variants for dealing with nested vectors when the display gets too tall or difficult to interpret.
Oddly, I also find it more keyboard-friendly, as typing "shift-T shift-9" comes to my fingers more readily than positioning the cursor where needed and typing "ctrl-shift-t," probably because it requires less hand movement.
Stuart
Nice, I like your many little helper functions which on first sight do not really much but at the end prove to be very handsome and helpful.
Wish one day I could succeed in creating my own library of utilities, being able to find it when I need and remember the function names i had chosen, rather than re-inventing the wheel from anew each time....
@Werner_E wrote:
Nice, I like your many little helper functions which on first sight do not really much but at the end prove to be very handsome and helpful.
Wish one day I could succeed in creating my own library of utilities, being able to find it when I need and remember the function names i had chosen, rather than re-inventing the wheel from anew each time....
I've now reached the stage where my new wheels get similar names. Plus, I have a "functions" Area in my default worksheet, where I keep my most useful or frequent functions.
If I get stuck for a name, I browse the MATLAB, Mathematica, Maple, numpy, or other application Help to find something close to what I want.
Here's an extract showing a more ... complex version of T that I use for vertically compacting vectors or nested arrays. It goes with saying that I need to carefully interpret what it's telling me, as it transposes column vectors but not row vectors (which would rather obviate T's purpose).
Naturally, I tinker with T when it doesn't quite meet my particular needs for viewing a nested array result.
Stuart
Guys @StuartBruff @Werner_E
How did you become so expert in mathcad like this?
Is there a course or book you learned from? or just be using mathcad daily ?
I look forward to using mathcad like you
@YA_10963798 wrote:
Guys @StuartBruff @Werner_E
How did you become so expert in mathcad like this?
Is there a course or book you learned from? or just be using mathcad daily ?
I look forward to using mathcad like you
I'm not sure I'd describe myself as "expert" (although my ego is busy preening itself, now).
But, yes, frequent use of Mathcad and learning from others in the Community.
... coupled to one of my many weaknesses - a predilection for generalising problems, playing, and tinkering.
I'm not aware of any current Mathcad tuition books. Perhaps @DJNewman can assist on this? I know there are some tutorial videos.
Hopefully, you'll become a far better Mathcadder than me! 🙂
Stuart
As far as I know (it's also the only Mathcad book I've read), the best book on the market for Mathcad Prime is still Brent Maxfield's Essential PTC® Mathcad Prime® 3.0: A Guide for New and Current Users.
You may observe that Mathcad Prime 3 is quite out-of-date, and a lot of things mentioned in it are either no longer true or there are better ways of doing it. But for very core things, it's still relatively good.
Otherwise, there is the Mathcad YouTube channel (the "PTC Mathcad Basic Info" playlist is meant for beginners, and then there are several other playlists on more specific and difficult topics), as well as the resources page on mathcad.com which includes links to webinars that range in intended experience level (so Mathcad for Civil Engineers is for beginners, while the Advanced Controls Crash Course is intermediate-level material, and Mathcad Prime 10 Numeric & Symbolic Engine Enhancements is probably advanced in the sense that it's way above my head), as well as other cool assets.
And this Community has links to three tutorial worksheets; Getting Started with PTC Mathcad Prime, Natural Math and Units in PTC Mathcad Prime, and Importing Data from Excel to Mathcad.
Plus, there are the three tutorials in Mathcad Help. That's where I personally started when I went to learn Mathcad Prime for the first time.
But if you want to get past the beginner stage, then, yeah, sticking around on PTC Community is the way to do it. Check out others' problems and especially the solutions posted. Try out the Mathcad Community Challenges too, and read and understand the solutions that other people submit. They're there to help you learn. (New challenge coming November 1. .........Hopefully. But the hyperlink in this paragraph also gives you the index linking to all the past ones and their "solution blogs" where all of the solutions are discussed. There's years-worth of innovative Mathcad use on display there.) You'll get more comfortable with Mathcad by using it and seeing how others use it.
Nice ... I think I will start with mathcad for civil engineers .. Thank you @DJNewman
@YA_10963798 wrote:
Guys @StuartBruff @Werner_E
How did you become so expert in mathcad like this?
Is there a course or book you learned from? or just be using mathcad daily ?
I look forward to using mathcad like you
As far as I'm concerned, I couldn't recommend a specific book, but I acquired my modest knowledge simply through curiosity, experimentation and frequent use of the programme.
Reading along in the pre-pre-predecessor of this forum, which was much better frequented, was particularly helpful. You could learn a lot from the answers to questions from experts like Stuart and a lot of others which are not here anymore for one reason or another.
Thank you Werner .. you always have been great help for me ..whenever I asked question I trust that you will be there helping . This is so gentle
Thank you .. I will use this ..looks very professional ...I like that you always take the answer to next level .
After a few minutes of thought, something started bugging me about exclude and select. There was something very familiar about the problem, but I couldn't put my finger on it, just vague sense of deja vu. So, I grabbed a cup of coffee and cast my mind back into deep time, and finally reached the dizzy depths of last week.
I'd been busy upgrading some M11 functions to Mathcad Prime 10, so they harkened to an era before trim. Consequently, I'd had to adopt a different approach, namely using a standard functional programming filter function. filter also solved another problem I'd hit more frequently than not: filtering matrices and ranges, which, as they stand, your exclude and select don't handle. However, it's an easy fix: use my ubiquitous vec function to flatten the matrices and expand the ranges. I've got another variant of filter that accepts a vector of indices and returns the corresponding elements (although I usually used a function called pick to do that job; sometimes, universality can go a little bit too far!).
But that wasn't the end of it, not by a long chalk. I'd become fed up with non-empty empty array stand-ins (a regular occurrence).. So, I reinvented the wheels known as stack and augment, called them stk and aug, and made them pretend they knew what an empty array was. It was quite pleasant not having to worry about initialising arrays before loops.
which then let me rewrite vec and filter accordingly,
And here are a few functions from another library worksheet I've got on the boil.
I haven't forgotten about the Expression Type Functions worksheet. However, answering your questions sent me off down a few other rabbit holes, and then I had some other ideas that needed addressing before I forgot them, and, etc, etc ... Fairly rapid progress is being made, though. and it might even be ready for publication in a couple of days.
Stuart
How can I show only the filtered results in a plot ?
Do you think if I download Prime 10 my work in prime 9 will be easily transferred? or I would have to start all over again
@YA_10963798 wrote:
Do you think if I download Prime 10 my work in prime 9 will be easily transferred? or I would have to start all over again
So far, in Mathcad Prime 10 (MP10), I haven't noticed anything "wrong" with your worksheets in terms of how they seem to work in Mathcad Prime 9. I'd suggest installing MP10 on another PC/laptop and checking your worksheets on that.
If you can't find another PC, contact your local IT people and have them back up your PC, install MP10, and then be on standby to do a restore if your testing reveals too many mistakes that you can't fix or would take too long to fix.
Stuart
The only thing that might be "screwed up" with opening Mathcad Prime 9 worksheets in Mathcad Prime 10 is if you had used the "undocumented trick" that was removed in Prime 10 of evaluating a range variable in the same line as defining it, in order to change its data type to become a variable.
Otherwise, it should be seamless.
Nope, nothing to do with Excel. That all works the same.
I mean...
versus
If you wanted an easy way to turn a range variable into a vector, you can borrow this suggested small program from CS416474.
@DJNewman wrote:
If you wanted an easy way to turn a range variable into a vector, you can borrow this suggested small program from CS416474.
The function Range2Vec is similar to many others that have appeared over the years, including my vec family of flatteners.
I have a problem with the name "Range2Vec".
Firstly, it takes more typing and use of the shift key. Think of the RSI ...
Secondly, functions that use this for-loop iteration over a variable do more than just convert ranges. As I said in a previous message, they'll convert matrices into vectors as well. My recursive version will flatten arbitrarily nested arrays into vectors.
The reason I chose the name vec is because a) it's short, easy to type, and easy to remember; b) it conforms with the mathematical operation of the same name, and as Mathcad is a mathy app, I feel that mathy names ought to apply where possible.
https://en.wikipedia.org/wiki/Vectorization_(mathematics)
In mathematics, especially in linear algebra and matrix theory, the vectorization of a matrix is a linear transformation which converts the matrix into a vector. Specifically, the vectorization of a m × n matrix A, denoted vec(A), is the mn × 1 column vector obtained by stacking the columns of the matrix A on top of one another:
I think it was Tom Gutman, one of the Mathsoft era Mathcadders extraordinaire, who first proposed the basic method, using the name ravel, possibly from some of the mathematically-based programming languages, such as APL,
Ravel (,) is a primitive function introduced in APL\360 which returns the ravel of an array. In the APL array model, an array's ravel is the vector containing all its elements in ravel order. It is equivalent to reshaping an array using its bound for the new shape. Reshaping the ravel using the original array's shape restores that array.
In some APLs an axis may be specified for Ravel in order to combine only some axes of an array, or insert a length-1 axis.
The name "ravel" references the process of undoing woven or knitted fabric, thus removing its structure and rendering it linear.
I forgot that people use index bases other than zero, so here are ORIGIN-independent versions of flat and recursive vec ...
Have I mentioned that Mathcad could really do with an empty array value? 😉
Stuart
@YA_10963798 wrote:
What is the undocumented trick ?Do you mean calling data from excel ?
No, it was to do with how some versions of Mathcad Prime dealt with the result of evaluating (= operator) a range variable when you defined it.
In MP10, k is still a range variable, but in MP9 k would be a vector and thus transposable. I rarely used the trick and, although I wasn't looking for it, I didn't notice any examples in your worksheets. It's an easy enough fix, though; if you need to use the name "k", just define the range variable as, say, "kk" and iterate over it to produce k as you would any other vector/array. Alternatively, use a simple version of vec to do the job without any name changes.
Of course, I could be wildly wrong about the undocumented trick - there are so many of them! 😉
Stuart
Hi Stuart,
I used the same function like yours , but I still get an error .... do you have any idea why?...
I need to get ztips as a vector so I can use it in this function
@YA_10963798 wrote:
Hi Stuart,
I used the same function like yours , but I still get an error .... do you have any idea why?...
What you show in this picture should work OK if you have ORIGIN set to 0 as it is per default.
As you usually have ORIGIN=1 in your sheets you must use
or simply ..+1, of course.
Concerning Stuarts extended version of "vec" you were already told whats wrong in the other thread
Re: vectorization - I need to change an array into... - PTC Community
@Werner_E @StuartBruff @DJNewman
Hi guys,
I installed mathcad prime 10 to another PC .. then opened my worksheet which is prime 9 there and I got this error message:
Is it only because my friend's laptop doesn't have license? or it has something to do with the version number?
The error message can't be read from that picture as of a too coarse resolution.
But if its saying something about Premium features not available, then your guess might be correct. The free Prime Express considers programming (and programming is used to calculate CPT) a Premium feature and will refuse to evaluate expressions using it.
You can use Prime Express as a reader - when you open your file you will see the state with all results which were calcuated the last time before you saved the file in the licensed version. But once you force Prime Express to recalculate (e.g. by changing an input value) it will refuse to evaluate expressions using Premium features and throw an error instead.
In the past Stuart Bruff and especially LucMeekes had sent a couple of very useful (for Express users) functions which replace some Premium functions and would still work in Express and they showed how programming can often be avoided by using the if-function instead of the programmed if-statement, by using recursion instead of explicit for loop and maybe also using undocumented functions like 'fold'.
Most of the times this makes the functions very hard and unclear to read, but they would work OK in Express, too.
Of course not very functionality of the full Prime version could be realized that way.
I don't think that it would be worth the effort to rewrite your whole worksheet, trying to make it work in Express as well ...
@Werner_E wrote:
The error message can't be read from that picture as of a too coarse resolution.
But if its saying something about Premium features not available, then your guess might be correct. The free Prime Express considers programming (and programming is used to calculate CPT) a Premium feature and will refuse to evaluate expressions using it.
You can use Prime Express as a reader - when you open your file you will see the state with all results which were calcuated the last time before you saved the file in the licensed version. But once you force Prime Express to recalculate (e.g. by changing an input value) it will refuse to evaluate expressions using Premium features and throw an error instead.
In the past Stuart Bruff and especially LucMeekes had sent a couple of very useful (for Express users) functions which replace some Premium functions and would still work in Express and they showed how programming can often be avoided by using the if-function instead of the programmed if-statement, by using recursion instead of explicit for loop and maybe also using undocumented functions like 'fold'.
Most of the times this makes the functions very hard and unclear to read, but they would work OK in Express, too.
Of course not very functionality of the full Prime version could be realized that way.I don't think that it would be worth the effort to rewrite your whole worksheet, trying to make it work in Express as well ...
Yes, Express-friendly functions can be somewhat cryptic.
Once a top level function's been written, however, it's generally little more burdensome to use than a programmed version of the same function. Local functions aren't an Express feature, but this can be worked around by breaking the main function up into a series of subfunctions. Indeed, full Prime requires this whenever a local function needs to be recursive (Prime broke recursive local functions a few versions ago).
OTOH, recursion is a useful tool on the right occasion and, indeed, can make some functions easier to implement and/or understand (once one has wrapped one's brain around the concept of recursion). I've just looked at a few Express and Prime equivalent functions, and it really was half a dozen of one and six of the other as to which was the "better" version. Even the recursive function performance was acceptable. It's certainly a fun game to play. 😎
Stuart
Hi Stuart,
Thank you for your comment , I downloaded prime 10 and it works . I hope it will be '' better version'' as you said.
Regards,
Yusra