Finding specific number of characters in vector

2 vues (au cours des 30 derniers jours)
Elijah L
Elijah L le 15 Sep 2020
Modifié(e) : Stephen23 le 15 Sep 2020
I have a character vector ( c = ['a' 'r' 'y' 'z' 'b' 'u' 'k'] ) and I would to determine the number of characters that are not b or r. How do I do this using logical operators?
  1 commentaire
Stephen23
Stephen23 le 15 Sep 2020
Modifié(e) : Stephen23 le 15 Sep 2020
Note that
c = ['a' 'r' 'y' 'z' 'b' 'u' 'k'];
is just a more complex and less efficient way of writing
c = 'aryzbuk';
You do not need to concatenate individual characters to make a character vector. It is totally superfluous. No experienced user would bother doing this.

Connectez-vous pour commenter.

Réponses (2)

Stephen23
Stephen23 le 15 Sep 2020
>> c = 'aryzbuk';
>> nnz(c~='b' & c~='r')
ans = 5

Ameer Hamza
Ameer Hamza le 15 Sep 2020
Try setdiff()
c = ['a' 'r' 'y' 'z' 'b' 'u' 'k'];
n = numel(setdiff(c, ['b' 'r']));

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by