Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of elements in matlab

Tags:

matlab

I am new to MATLAB. Suppose I have a vector like x = [1 1 1 1 1 1 0 0 1 0]. I want to calculate the total number of elements in the vector and the number of non zero elements in the vector. Then come up with a ratio of both the numbers. I am searching in MATLAB help. how to do count of elements, but till now I didn't get any luck. If anyone provide me with help, it would be of great help. Thanks in advance.

like image 715
user537670 Avatar asked Dec 07 '22 21:12

user537670


1 Answers

You can get the number of elements with numel(x).

You can get the number of non-zeros with sum(x ~= 0).

So the ratio is one divided by the other.

like image 189
Oliver Charlesworth Avatar answered Dec 29 '22 02:12

Oliver Charlesworth