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

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

Translate the entire conversation x

Exclude some values

YA_10963798
14-Alexandrite

Exclude some values

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 

YA_10963798_1-1730037766728.png

How to fix this ?

 

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:Werner_E)

You may also use a generic utility function to do the job:

Werner_E_0-1730044894764.png

You may then use either

Werner_E_2-1730041443456.png

or in one go

Werner_E_4-1730041480344.png

 

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

Werner_E_1-1730041177071.png

 

View solution in original post

50 REPLIES 50
Werner_E
25-Diamond I
(To:YA_10963798)

Delete the word "rows", just use     if d[i < width

 

A simpler way probably would be to just use

Werner_E_7-1730042081725.png

or

Werner_E_8-1730042183264.png

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.

 

 

 

Werner_E
25-Diamond I
(To:Werner_E)

You may also use a generic utility function to do the job:

Werner_E_0-1730044894764.png

You may then use either

Werner_E_2-1730041443456.png

or in one go

Werner_E_4-1730041480344.png

 

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

Werner_E_1-1730041177071.png

 

StuartBruff
23-Emerald III
(To:Werner_E)

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.  

 

2024 10 27 A.png

 

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

Werner_E
25-Diamond I
(To:StuartBruff)

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....

StuartBruff
23-Emerald III
(To:Werner_E)


@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).  

 

2024 10 27 B.png

 

Naturally, I tinker with T when it doesn't quite meet my particular needs for viewing a nested array result.

 

Stuart

YA_10963798
14-Alexandrite
(To:StuartBruff)

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 

StuartBruff
23-Emerald III
(To:YA_10963798)


@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  

YA_10963798
14-Alexandrite
(To:StuartBruff)

I see .. so I will keep asking till I learn more ..
thank you Stuart .. BTW you are so humble , but always provide amazing answers..

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.

I manage the Creo and PTC Mathcad YouTube channels for PTC, as well as all PTC Mathcad marketing in general.
YA_10963798
14-Alexandrite
(To:DJNewman)

Nice ... I think I will start with mathcad for civil engineers .. Thank you @DJNewman 

Werner_E
25-Diamond I
(To:YA_10963798)


@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.

 

YA_10963798
14-Alexandrite
(To:Werner_E)

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 

YA_10963798
14-Alexandrite
(To:Werner_E)

Thank you .. I will use this ..looks very professional ...I like that you always take the answer to next level . 

 

StuartBruff
23-Emerald III
(To:Werner_E)

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!).

 

2024 10 28 C.png

 

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.

 

2024 10 28 B.png

 

which then let me rewrite vec and filter accordingly,

 

2024 10 28 A.png

 

And here are a few functions from another library worksheet I've got on the boil.

 

2024 10 28 D.png

 

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

YA_10963798
14-Alexandrite
(To:StuartBruff)

How can I show only the filtered results in a plot ? 

YA_10963798
14-Alexandrite
(To:StuartBruff)

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

StuartBruff
23-Emerald III
(To:YA_10963798)


@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.

I manage the Creo and PTC Mathcad YouTube channels for PTC, as well as all PTC Mathcad marketing in general.
YA_10963798
14-Alexandrite
(To:DJNewman)

What is the undocumented trick ?
Do you mean calling data from excel ?


Nope, nothing to do with Excel. That all works the same.

 

I mean...

 

Mathcad Prime 9; turning a range variable into a vectorMathcad Prime 9; turning a range variable into a vector

 

versus

 

Mathcad Prime 10.0.1.0, you can't turn a range variable into a vector anymoreMathcad Prime 10.0.1.0, you can't turn a range variable into a vector anymore

 

 

If you wanted an easy way to turn a range variable into a vector, you can borrow this suggested small program from CS416474.

I manage the Creo and PTC Mathcad YouTube channels for PTC, as well as all PTC Mathcad marketing in general.
YA_10963798
14-Alexandrite
(To:DJNewman)

I see . Thank you for the explanation

StuartBruff
23-Emerald III
(To:DJNewman)


@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 ...

 

2024 10 28 G.png

 

Have I mentioned that Mathcad could really do with an empty array value?  😉

 

Stuart

 

 

DJF
16-Pearl
16-Pearl
(To:DJNewman)

My company upgraded to 10 this week so I just discovered the fact that the undocumented range to vector trick no longer works.  I now have many hundreds of documents that will need updating, but that's another story.  Looking at several threads I see different approaches to replace this 'feature', but they all seem to deal with integers and not be an exact replacement for what was lost.  So this is what I came up with.  Maybe it will help someone or maybe someone will point out a better wayrange2vec.png.

Werner_E
25-Diamond I
(To:DJF)

I won't call your function a "range 2 vec" function because it does not convert an already defined range into a vector but rather creates a vector from scratch using the same three values (first, second, last) as are used by the usual range creation.

There are various "range 2 vec" functions posted here in the forum from very basic ones up to quite sophisticated solutions (from Stuart, e.g. see here: https://community.ptc.com/t5/Mathcad/vectorization-I-need-to-change-an-array-into-vector-in-Prime-10/m-p/984350/highlight/true#M215108).

A basic range2vec function is the "vec" function shown here:

Werner_E_0-1746022636353.png

So instead of using the undocumented trick with the inline evaluation (which worked up to Prime 9)

Werner_E_2-1746022707018.png

 

you now first define the range as you are used to do and then in a second step use the "vec" function to turn it into a range:

Werner_E_3-1746022782959.png

 

You might be interested to know that Prime 11 contains a built-in "vec" function which, as far as I had read, can be used to define a vector similar to your your function as well as converting a range into a vector like the vec-function I showed here.

I understand why PTC has stopped the undocumented trick to work (its confusing and illogic that an evaluation changes the data type) but its a pity that they did not provide the alternative ("vec" function) at the same time.

 

EDIT: I Just noticed that in his answer just below Stuart provided the same "vec" function as I did. He omitted the initialisation of V because a variable used in the left hand side of an assignment in a program automatically is treated as local variable, so a worksheet variable with the same name would do no harm. His version assumes the default  ORIGIN=0.

As Stuart has shown you may do the whole thing in one go by using the range definition as the function argument. Note the extra pair of parenthesis which is necessary as otherwise Prime would not allow a range as function argument. Prime will not allow to type in the range but with that extra parenthesis at least you can add the range argument via copy and paste:

Werner_E_4-1746024112980.png

 

 

LucMeekes
23-Emerald IV
(To:DJF)

Your function is equivalent to:

LucMeekes_0-1746023404501.png

Which has the advantage to also work in Prime express.

Example:

LucMeekes_1-1746023419650.png

 

Success!
Luc

 

Werner_E
25-Diamond I
(To:LucMeekes)

@LucMeekes 

Try 0.003 in  or even 0.0031 in as "high" value ...

 

Apart from some roundoff  error we may get, especially when using units, this should work OK

Werner_E_0-1746025070964.png

Here an example where this approach (and also the function @DJF  provided) fails

Werner_E_1-1746025664961.png

Relying on Prime creating a correct range and then converting it into a vector usually is the safer way:

Werner_E_2-1746025721300.png

 

 

 

 

LucMeekes
23-Emerald IV
(To:Werner_E)

Yes, edge cases.

Otherwise this will do (probably with other edge cases):

LucMeekes_0-1746025043063.png

LucMeekes_1-1746025062067.png

 

Success!
Luc

 

Werner_E
25-Diamond I
(To:LucMeekes)

No, this version is worse IMHO.

While the other version (with trunc or maybe better floor and +1) should work but fails because of round-off/conversion errors, this version will not work correct by concept if the last value equals 'high':

Werner_E_1-1746027403914.png

 

Some time ago I myself had played around with functions creating a vector by providing <first. second, last> or also <first, step, last> or <first, step, number of elements> and also <first, last, number of elements> but never could come up with a satisfactory result as I always found cases where one value would be missing or is superfluous. Mainly because of internal conversion or round-off problems. So I found the built-in range creation algorithm to be more reliable and settled with creating ranges and then converting them to vectors.

I have not yet tested the new "vec" function in P11 but I hope that its as reliable as the range creation algorithm was so far.

StuartBruff
23-Emerald III
(To:YA_10963798)


@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.

 

StuartBruff_0-1730132202358.png

 

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.

 

StuartBruff_1-1730132625663.png

 

Of course, I could be wildly wrong about the undocumented trick - there are so many of them!  😉

 

Stuart

Announcements


Top Tags