Finding specific number of characters in vector

3 ビュー (過去 30 日間)
Elijah L
Elijah L 2020 年 9 月 15 日
編集済み: Stephen23 2020 年 9 月 15 日
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 件のコメント
Stephen23
Stephen23 2020 年 9 月 15 日
編集済み: Stephen23 2020 年 9 月 15 日
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.

サインインしてコメントする。

回答 (2 件)

Stephen23
Stephen23 2020 年 9 月 15 日
>> c = 'aryzbuk';
>> nnz(c~='b' & c~='r')
ans = 5

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by