Is it possible to use logical indexing to specify between a number interval and include a value outside that interval.

1 回表示 (過去 30 日間)
I'm trying to use logical vectoring to exclude certain characters by using a mask on a string. So say I had a string like str='This !is @tes#t'. I'm trying to get "This is test". I'm donig this by converting the strings to doubles and then setting all characters to upper. Finally creating a mask that is between 65 and 90. I also need the mask to include 32 so I can keep spaces between words. How can I include that? Alternatively is there a better way to only include the alphabetic characters in a mask?
str='This !is @tes#t'
Ustr=upper(str)
Mask = 65<=Ustr & Ustr<=90

採用された回答

James Tursa
James Tursa 2019 年 1 月 31 日
編集済み: James Tursa 2019 年 1 月 31 日
E.g.,
Mask = ismember(str,[' ','a':'z','A':'Z']);
Or using your method
Mask = (65<=Ustr & Ustr<=90) | Ustr == 32;
  1 件のコメント
Confidential Confidential
Confidential Confidential 2019 年 1 月 31 日
Thank you. The pitful of being new is you don't even begin to know the functions of the language.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2019 年 1 月 31 日
You could use the isstrprop function to keep alphabetic letters and whitespace characters.
str='This !is @tes#t';
letters = isstrprop(str, 'alpha');
spaces = isstrprop(str, 'wspace');
str(letters | spaces)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by