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

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Logical Indexing Functionality

astevenson-2
1-Visitor

Logical Indexing Functionality

I'm a MathCAD 15 user. Does MathCAD have logical indexing function? In other words I want to find the indices in a matrix where the values of the matrix are within some criteria. For instance I would like to find the row and column numbers in the matrix below that are between -3.8 < G(i,j) < 2.3? Sure I could write a small function to do this but if I were using Matlab A=G(G<2.3 & G>-3.8) would get me what I need.

Capture.JPG

ACCEPTED SOLUTION

Accepted Solutions

Much nicer version.

Slight alternative to my version (plagiarising Richards method) below with nested matrix removed.

Mike

View solution in original post

14 REPLIES 14

Something like the attached?

Mike

How about this:

Much nicer version.

Slight alternative to my version (plagiarising Richards method) below with nested matrix removed.

Mike

Thanks Richard and Mike. Both your responses will work and I'll use them but they both lack the slickness of Matlab's logical indexing.

Andy Stevenson wrote:

Thanks Richard and Mike. Both your responses will work and I'll use them but they both lack the slickness of Matlab's logical indexing.

They do.  On the other hand, if you write a generic function, you could save it, along with your favourite 'go to' functions, in a collapsed Area on your Normal (or some other) Template, which means it would be less of a pain to use next time you needed that capability.  Eg, using Richard's example matrix, here's one from my Utilities toolbox: matchp (match predicate) takes a predicate function and returns the indices of all elements of an array for which the predicate function is true.  I also normally replace the built-in match function with one that returns 0 if there are no matches ... this makes checking for no matches a simple matter for an if function or if statement.

This is one of those rare instances where Prime has an advantage over M15, as Prime's dynamic type checking, coupled to use of IsFunction, allows you replace the match function with a more capable variant.

I do wish Prime's programming constructs wouldn't occupy so much vertical space; it makes a program (and worksheet) more difficult to read than needed.   At the very least, there should be a functional equivalent of try (as per the if function), eg try(match(z,A),0).

Stuart

Hi Stuart,

Have a tried to replicate your matchp(p,A) function to no avail. Could you post the worksheet please.

Thanks,

Mike

Mike Armstrong wrote:

Hi Stuart,

Have a tried to replicate your matchp(p,A) function to no avail. Could you post the worksheet please.

Sure. No problem, Mike; I've attached a Prime 3.1 version as well.  Here's some other match related functions that I've found useful on occasion (actually, I use the new match in place of the built-in match all the time).

Stuart

StuartBruff
23-Emerald III
(To:StuartBruff)

StuartBruff wrote:

Sure. No problem, Mike; I've attached a Prime 3.1 version as well.  Here's some other match related functions that I've found useful on occasion (actually, I use the new match in place of the built-in match all the time).

Stuart

Although, of course, I should have rewritten match to include the predicate version ... I think this should be possible in M11 as well (although there would have to be a different method of checking if z is a function, as there is no IsFunction in Mathcad <=15.

Stuart

Thanks Stuart. The material you provide on this forum is priceless.

Mike

Mike Armstrong wrote:

Thanks Stuart. The material you provide on this forum is priceless.

Mike

True, Mike.  Which annoys the heck out of my wife, as she thinks it should be priced!  I told her I can't afford to pay people to look at it.

Still, it's nice to know that my postings are potentially of some value to others.  Thanks. 

I note, though, that I have some way to go to catch you up on the value stakes, given your position on the "Leader Board"!

Stuart

StuartBruff wrote:

I note, though, that I have some way to go to catch you up on the value stakes, given your position on the "Leader Board"!

Stuart

Quality not quantity

Mike

Mike Armstrong wrote:

StuartBruff wrote:

I note, though, that I have some way to go to catch you up on the value stakes, given your position on the "Leader Board"!

Stuart

Quality not quantity

Mike

Quality and Quanity!  The same as the others up there.

Stuart

Stuart reminds me of the old hymn,

     Come, Thou Fount of every program,

     Tune my heart to program like thee;

     Streams of code, never ceasing,

     Call for songs of loudest praise.

Andy Stevenson wrote:

Stuart reminds me of the old hymn,

    Come, Thou Fount of every program,

    Tune my heart to program like thee;

    Streams of code, never ceasing,

    Call for songs of loudest praise.

I note the original words

Come, Thou Fount of every blessing,

Tune my heart to sing Thy grace;

Streams of mercy, never ceasing,

And further note that Grace Hopper was the moving force behind COBOL, which suggests

Cobol, Thou Fount of every cursing,

Tune my heart to sing to Grace;

Screams of mercy, never ceasing,

(Fortunately, I've never programmed in COBOL, but I've heard of Real Programmers hanging themselves from the rafters rather than work on Accounts Received in COBOL).

Stuart

Announcements

Top Tags