MATLAB Answers

Counting distinct elements in a vector?

3 views (last 30 days)
James Baker
James Baker on 8 May 2021
Commented: James Baker on 9 May 2021
ok lets say i have a vector A = (1 1 2 2 3 4 4 4 4 5 7 7 7) the original A vector was related to actual time, hence why its increasing....i've already used the ceil function to bucket the original elements into distinct minutes... now i want to count up the elements that occur in distinct minutes.. so, for the example above- I expect a final result of 6....i cant figure out how to come up with that code.. please help

Accepted Answer

Matt J
Matt J on 8 May 2021
Edited: Matt J on 8 May 2021
numel(unique(A))
or
max(findgroups(A))
  4 Comments
James Baker
James Baker on 9 May 2021
whoops he B vector should be (2 2 1 4 1 3)... not (2 2 1 4 5 3)... doh!

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!