Some MATLAB help needed !
I have an set of 1s and 0s, I need to find how many 1s and how many 0s.
(i.e. x = [ 1 1 0 0 0 0 0 1 0 0 1 1 ....] ) . I was looking at some search and count inbuilt function, however I have not been successful.
Some MATLAB help needed !
I have an set of 1s and 0s, I need to find how many 1s and how many 0s.
(i.e. x = [ 1 1 0 0 0 0 0 1 0 0 1 1 ....] ) . I was looking at some search and count inbuilt function, however I have not been successful.
What about the built-in sum
and length
functions, i.e.
numOfOnes = sum(x);
numOfZeros = length(x)-numOfOnes;
This assumes that you really only have 0s and 1s in your vector. If you can have different values but want to count the 0s and 1s only, you can preprocess the vector and count the 1s in a logical vector:
numOfOnes = sum(x==1);
numOfZeros = sum(x==0);
You can just do
onesInList = sum(x == 1);
zerosInList = sum(x == 0);
This extends to any values you have in the list (i.e., if you wanted to find all of the sevens, you could just do sevensInList = sum(x == 7);
).