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

How do I extract elements that are less than 0 or bigger than 0 in a vector?

Highlighted
Newbie

How do I extract elements that are less than 0 or bigger than 0 in a vector?

Hi Everyone,

I'm new here, so i dont know if i written this question under the right subjekt. I will start with saying that I'm sorry for my bad english.

Now to my question:

I'm sitting right now with a projet where i'll try to make a template to calculate some buildning structures. When i Do a bending moment controll i get an array (a vector) which has negative values and positive values in it. How do i in simple way extracting those elemetns which is less than 0 in a own array?

Like if i have a Vector A: [0

                                         10

                                          -5

                                          -3

                                          6]

And i want to extract just the negative values and end up with a vector like this:

[-5

-3]

How do I do?

I have searching in this forum but I haven't found a solution yet.

Tags (2)
4 REPLIES 4
Highlighted

Re: How do I extract elements that are less than 0 or bigger than 0 in a vector?

You have to sort through the vector element by element.  This is best done in a program; unfortunately Prime Express doesn't support programming (and a lot of other useful things.)

Which version of Mathcad are you using?

Highlighted

Re: How do I extract elements that are less than 0 or bigger than 0 in a vector?

You could use a combination of "match" and "trim" to do the job

But this line will fail if no negative values are present

so we will use "onerror" (in Prime you would use "try") to catch that error and return the vector unchanged

But there is still a situation, where we will get unwanted results. If ALL values in the vector are negative, nothing will be left. This throws an error and because errors are trapped by "onerror" now we get back the original vector, which is not the desired behavior.

So I thought that now its time to write an utility function to do the job and call later when needed (I have chosen to return NaN in the last case, change as appropriate)

Hope this helps

Werner

Highlighted

Re: How do I extract elements that are less than 0 or bigger than 0 in a vector?

For completeness sake here the Prime version

The basic line (first one in my answer above) will work in Prime Express, too, but for the error catching you need the full version because "try" is a programming command and programming is considered a premium feature which is not enabled in the free Express variant.

EDIT: Just found that the "trim" command is also considered a premium feature, so not even the basic line won't work in Express 

And just for fun here is an alternative, classic approach, going through the vector element by element and collecting the appropriate elements for the new, trimmed vector


WE

Highlighted

Re: How do I extract elements that are less than 0 or bigger than 0 in a vector?

Thank you for your answers!

I will look in to it! My university has MathCAD prime ver 3.1 on the computers in the computer lab. So this will absolutly help.

Best regards

Announcements