MATLAB Answers

Finding specific number of characters in vector

1 view (last 30 days)
Elijah Lavancher
Elijah Lavancher on 15 Sep 2020
Edited: Stephen Cobeldick on 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 Comment

Stephen Cobeldick
Stephen Cobeldick on 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.

Sign in to comment.

Answers (2)

Stephen Cobeldick
Stephen Cobeldick on 15 Sep 2020
>> c = 'aryzbuk';
>> nnz(c~='b' & c~='r')
ans = 5

  0 Comments

Sign in to comment.


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

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!