Skip to main content
1-Visitor
September 13, 2016
Solved

Count Values in a Matrix

  • September 13, 2016
  • 4 replies
  • 7613 views

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

Best answer by RichardJ

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

4 replies

RichardJ19-TanzaniteAnswer
19-Tanzanite
September 13, 2016

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

acharest1-VisitorAuthor
1-Visitor
September 13, 2016

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?

19-Tanzanite
September 13, 2016

No problem:

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

23-Emerald IV
September 14, 2016

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

23-Emerald IV
September 14, 2016

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

24-Ruby III
September 14, 2016

Hi,

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

matice.png

MH

25-Diamond I
September 14, 2016

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