assume i have folllowing vector column named as ALT_digit_
i want to know the number of times 'hi' comes in the vector column.

 Respuesta aceptada

KSSV
KSSV el 10 de Feb. de 2022

0 votos

LEt A be your cell array.
idx = contains(A,'hi') ;
nnz(idx)

Más respuestas (2)

Adam Danz
Adam Danz el 10 de Feb. de 2022
Editada: Adam Danz el 10 de Feb. de 2022

0 votos

See strcmp (case senstive) or strcmpi (case insensitive) to compare a vector of strings to a single string. Those functions will return a logical vector. Then add the vector using sum() to count the number of matches.
n = sum(strcmpi(T.ALT_digit, 'hi'))

1 comentario

Adam Danz
Adam Danz el 10 de Feb. de 2022
If your only string values are 'hi' 'lo' or '' and your Matlab release is R2016b or later, this solution is perfectly fine. But if you're using a release prior to 16b or combinations of 'hi' and 'lo', consider using a different method.

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 10 de Feb. de 2022

0 votos

Yes another way : using ismember():
ALT_digit = {'lo', '', '', '', 'lo', '', '', '', 'lo', '', 'abc'}
rows = ismember(ALT_digit, 'lo')
count = sum(rows)

Categorías

Etiquetas

Preguntada:

el 10 de Feb. de 2022

Comentada:

el 10 de Feb. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by