Clear Filters
Clear Filters

how to count occurrences of a variable in a matrix?

1 view (last 30 days)
Hi, I am stuck with my problem.
I have a matrix A = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 'Red', 'Black', 'Purple', 'Red', 'White'}.
Now I want to count occurrences of 'Red' in this matrix. But I don't know how to count it. Please help me resolve this problem!
Thanks and Best Regards,
Kiet Vo

Accepted Answer

KSSV
KSSV on 6 Aug 2021
A = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 'Red', 'Black', 'Purple', 'Red', 'White'} ;
nnz(strcmp(A,'Red'))
ans = 3
  1 Comment
Võ Kiet
Võ Kiet on 6 Aug 2021
nice!!! Thanks you so much, you saved me. Again, thanks so much.
Best Regards,
Kiet Vo

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!