Does anyone know how to count the number of times a value appears in a matrix?
For example, if I have a 1500 x 1 matrix M
(vector) which stores the values of weekdays (1 - 7), how could I count how many Sundays (1), Mondays(2), ... , Saturdays(7) are stored in M
?
A matrix is said to be of order m x n if it has m rows and n columns. The total number of elements in a matrix is equal to (m*n). So we start from 1 and check one by one if it divides N(the total number of elements).
n = numel( A ) returns the number of elements, n , in array A , equivalent to prod(size(A)) .
If for example your matrix is A, you can use : size(A,1) for number of rows. size(A,2) for number of columns. Also there are some other ways like : length ( A(:,1) ) for number of rows.
Use the [row,column] = size(matrix); to directly obtained the number of columns and rows of our matrix from this code through the size() function.
Have a look at Determine and count unique values of an array.
Or, to count the number of occurrences of 5
, simply do
sum(your_matrix == 5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With