Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X
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
Solved! Go to Solution.
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?
No problem:
The "on error" statement is from the programming toolbar, you can't just type it.
Thanks for the help, that worked great!
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
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
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