How to use logical "OR" operation for comparing string??

I tried with the code as given:
strcmp('normal'||'neptune',{'normal'; 'DoS'; 'neptune'; 'R2L'})
and the error is saying that the OR "||" operator must be convertible to logical scalar values. So, what could be the possible solution to this?? thanks in advance.

 採用された回答

Walter Roberson
Walter Roberson 2017 年 12 月 15 日

0 投票

any( ismember({'normal', 'neptune'}, {'normal'; 'DoS'; 'neptune'; 'R2L'}) )

4 件のコメント

Shamim
Shamim 2017 年 12 月 15 日
編集済み: Shamim 2017 年 12 月 15 日
I need the index of true positions such as [1 0 1 0] or [1 3] but it's saying only true no false positions are provided.
Walter Roberson
Walter Roberson 2017 年 12 月 15 日
ismember({'normal', 'neptune'}, {'normal'; 'DoS'; 'neptune'; 'R2L'})
However, you had used the || operator, suggesting you were expecting a scalar result, since || is only used to connect scalars. We had reason to expect that you were looking for a single output rather than a vector of outputs.
Walter Roberson
Walter Roberson 2017 年 12 月 15 日
For the index positions,
[tf, idx] = ismember({'normal', 'neptune'}, {'normal'; 'DoS'; 'neptune'; 'R2L'})
The returned idx would be [1 3] in this example.
idx(K) will be valid only in the locations where tf(K) is true (and will be 0 otherwise.)
Shamim
Shamim 2017 年 12 月 15 日
編集済み: Shamim 2017 年 12 月 15 日
Hello Mr. Roberson first of all thank you very much for your help and secondly I guess that, I didn't explain my problem properly. The code above is returning the first true index position only. If the same string is repeated again in the cell array then it's not returning those positions.
In attached picture I showed two commands and my intention was to merge those two commands into one because my real operation is much more bigger than this and it will also help me to reduce 32 if else condition into one single command.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

質問済み:

2017 年 12 月 15 日

編集済み:

2017 年 12 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by