フィルターのクリア

Hey, I am having trouble indexing into string vectors

2 ビュー (過去 30 日間)
Andrzej Mamczura
Andrzej Mamczura 2020 年 4 月 27 日
コメント済み: Steven Lord 2020 年 4 月 27 日
I am having trouble indexing into string arrays using logical values. As an example:
res = ["ABC" , "DEF" , "GHI"]
tf = [ 1 0 1 ]
x = res(tf)
I expect MATLAB to return
x = ["ABC" , "GHI"]
however instead i get the following error
"Array indices must be positive integers or logical values."

採用された回答

Sriram Tadavarty
Sriram Tadavarty 2020 年 4 月 27 日
Hi,
Update this as below:
res = ["ABC" , "DEF" , "GHI"]
tf = [1 0 1]; % This is of type double, so convert to logical as below
x = res(tf == 1);
Hope this helps.
Regards,
Sriram
  2 件のコメント
Andrzej Mamczura
Andrzej Mamczura 2020 年 4 月 27 日
Thanks so much! Exactly what I was looking for.
Steven Lord
Steven Lord 2020 年 4 月 27 日
Many functions and pieces of functionality in MATLAB treat 1 and true as roughly equivalent and the same for 0 and false.
Indexing is not one of those pieces of functionality.
If you wanted to avoid having to compare tf each time you wanted to use it as an index, I'd create it as a logical array.
res = ["ABC", "DEF", "GHI"];
tf = [true false true]
res(tf)

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

その他の回答 (1 件)

Anna Case
Anna Case 2020 年 4 月 27 日
tf = [1 3] is what you are looking for. If you open up the variable res, you will see "ABC" is in index 1 and "GHI" is in index 3.

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by