function count_zeros(M) % Counts the number of zero and non-zero elements in a matrix M. % % To use this function type `count_zeros(M)` where M is a matrix. n = size(M,1); m = size(M,2); zeros = 0; nonzeros = 0; for i = 1:n for j = 1:m if (M(i,j) == 0) zeros = zeros + 1; else nonzeros = nonzeros + 1; end end end zeros = zeros nonzeros = nonzeros