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

Community Tip - You can change your system assigned username to something more personal in your community settings. X

Exclude some values

YA_10963798
12-Amethyst

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

36 REPLIES 36
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

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  

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.

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.

 

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 . 

 

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

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

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.

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.

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

 

 

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

Werner_E
25-Diamond I
(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


My guess is that you won't experience any problems if you do, but then on the other hand its always a risk to change the horses in-midst of a race.

We have seen her in the forum that Prime 10 'introduced' some new bugs, some of which were fixed in the current 10.0.1.0, other may still remain. As far as I am aware these bugs mainly concerned the symbolic engine (which you seem not to use anyway) and function with a lot of arguments. The latter is something you don't use anyway as far as I can tell and seems to be one of the bugs already fixed in 10.0.1.0.

But you can give it a try anyway - when you install Prime 10 this does not mean that you lose Prime 9. Both versions can coexist on the same machine. Only the converter (for legacy Mathcad files) would not be able to create Prime 9 files anymore and of course once you save a file in P10 you will never be able to open it in P9 again or convert it back to P9 format!

The undocumented (and unsupported) method mentioned by DJNewman to turn a range into a vector by a simple inline evaluation will not work in P10 anymore. While it was illogical that an evaluation would change the data type, it was a very handy and convenient method and I would had wished that PTC would provide an as convenient built-in alternative (the various Mathcad functions have their drawback, especially when used inside a program).
But as far as I can remember, you did not use this method in your sheet anyway.

Hi Werner @Werner_E 

I'm trying to show the results the same way you did , but error occurs . It didn't work once I add "row" , though I used your way and Stuart way , 

 

YA_10963798_1-1730193289701.png

YA_10963798_3-1730193620503.png 

this is result3 

 

YA_10963798_0-1730193242252.png

I need to show the rows of the results and then draw it the same way you draw the this one , but only with the selected values 

 

YA_10963798_2-1730193454137.png

 

can you show me how ?

Werner_E
25-Diamond I
(To:YA_10963798)

This is now not a question concerning "Exclude some values", isn't it.
It rather a question belonging to the thread about the nearest point.

I was not ware that you also needed the rows number of that nearest point because you did not specify it in your intial posting there. You just said that you need the nearest point and the distance.

Its not difficult to modify the functions to also provide the index of the point.

Here done with the first 'table' (function 'getNearest') I provided in that thread

Werner_E_0-1730204275469.png

 

Ok that was easy... I spent 2 hours thinking how to do that hhh . I even wrote different function.. Thank you

Werner, I'm so sorry to ask so many question...I promise this is my last question in this thread 

I'm trying to write a new program to show the selected points ... the word "result 3" is the exclued values... but obviously what I wrote  is wrong

I'm trying to write the same program , so then I can plot it as we draw it before ..

YA_10963798_0-1730212835553.png

I want to show the selected values in different color in this plot 

YA_10963798_1-1730213020424.png

 

 

Werner_E
25-Diamond I
(To:YA_10963798)

The plot I made out of fun was meanly meant to be used for my demo data with just a couple of points in each vector. It should demonstrate that its possible that two or more points in A have the very same minimal distance from a point in C as the question as to what to do in this case was not (and still isn't) answered. My "table" used just the first of the multiple point which may be possible, "table2" would show all of them which is the reason why the "A point list" in table2 are vectors as there could be more than one nearest point for a specific C-point.

So the reason for the plot was not only to show the points but mainly to connect corresponding points with straight lines.
This did not make much sense with the original data so I zoomed in by adjusting the axis limits manually (Prime unfortunately does not offer a zoom facility).
Without zooming it would look like this. The connections between the corresponding points would not be seen.

Werner_E_0-1730225652086.png

 

I also noticed that with the Excel data you provided there is no single point in C with more than only one nearest A-point. If this is always so for sure or if it suffices to use the first one if there would be more than one point with different input data, I guess that "table" is preferable over "table2" because its not nested and so makes for a nicer display. Of course  there may be reasons for rather using  a structure as in "table2" or as the one Stuart had provided. Its up to your choice.

 

Concerning the plot I am not sure what you would like to plot.

I have not looked into detail in the sheet you posted last as it was a bit too confusing for me with all the different suggestions for the nearest points mixed up.

I look at result3 now and its just a vector of 1308 distance values. So what exactly would you plot here? I am not sure what you would like to see in the plot.

Guess you would first decide upon the data format for A and C.CPT you want to use (either still nested or flattened), decide on one of the methods to create the nearest point list and then chose a way to extract whatever points or distances you need.

Maybe cleaning up the worksheet in this respect would help.

Form the picture you posted its not clear to me what your function "new" is supposed to create with its three nested for-loops.

Announcements

Top Tags