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

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

Count Values in a Matrix

acharest
1-Newbie

Count Values in a Matrix

Hello,

I'm new to MathCAD and may have missed something obvious, but is it possible to count particular values in a matrix?  For example, counting how many times the number "18" occurs in a 1 x 5 matrix.

Thanks,

Adrian

ACCEPTED SOLUTION

Accepted Solutions
RichardJ
19-Tanzanite
(To:acharest)

It's not only possible, it's very easy;

View solution in original post

8 REPLIES 8
RichardJ
19-Tanzanite
(To:acharest)

It's not only possible, it's very easy;

But if the value isn't present MathCAD returns an error.  Sorry for not being clear, is there a way to return "0" if the value is not there?

RichardJ
19-Tanzanite
(To:acharest)

No problem:

The "on error" statement is from the programming toolbar, you can't just type it.

Thanks for the help, that worked great!

LucMeekes
23-Emerald III
(To:acharest)

There are more roads that lead to Rome...

Hist will count the number of elements of its second argument (M in this case), that are within the intervals as described by its first argument (the vector with elements 0 through 9, note that 0.5 is added to each element of this vector).

From the help:

hist(intvls,data) Returns a histogram of data - a vector representing the frequencies with which values in data fall into the intervals represented by intvls.

Note that this method will count 1.1 as well as 0.9 in the bin for 1's, like wise for the other numbers.

Success!
Luc

LucMeekes
23-Emerald III
(To:LucMeekes)

And then there's this:

histogram(intvls,data) Returns a matrix with two columns. The first column contains midpoints of the n subintervals of the range min(A) <= value <= max(A) of equal length. The second column is identical to hist(n, A), and hence the resulting matrix has n rows. Using histogram rather than hist saves you the additional step of defining horizontal axis data when plotting.

Luc

Hi,

in Mathcad 3.1 I created this "thing" ...

matice.png

MH


Martin Hanák
Werner_E
25-Diamond I
(To:acharest)

Looks like we are going to collect differnt ways and methods to achieve the same result.

So here is another piece for that collection

Guess we could also combine Stuarts "unravel" with the vector sum operator.

Nevertheless I would vote for Richards approach - nice and tidy 😉

Werner

Announcements

Top Tags