Finding specific number of characters in vector

조회 수: 2 (최근 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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by