How to count number of states in array?

3 views (last 30 days)
Hi,
I want to count number of states in vector, each number represent a state: Ex.
A = [1;1;0;1;0]
B = [2;2;1;0;1]
C = [1;1;1;1;1]
A has two states 1 and 0 ==> k=2, B has three states 1, 2, 0 ==> k=3, and C has zero state ==> k=0. how can I write a proforma to define k (number of states)?
  3 Comments
Walter Roberson
Walter Roberson on 4 Jun 2017
If necessary, my suggestion could be followed by testing if the result was 1, and if so then change it to 0.
AbuYusuf
AbuYusuf on 5 Jun 2017
you are right Les, I made a mistake in my question. For C, it should be one state.
I was looking exactly to the line of code which Wlater suggested. Thanks both for your comments.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 3 Jun 2017
length(unique(TheVector))

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!