how to label strings in an array with an ID

5 ビュー (過去 30 日間)
Ryan Rose
Ryan Rose 2020 年 8 月 3 日
コメント済み: Ryan Rose 2020 年 8 月 3 日
I have a array
StringID=['a',1;
'b',2;
'c',3]
and a table with a column with a bunch of strings that exactly match the strings in StringID.
tc=['c';
'b';
'c';
'a';
'c']
How can I make a column vector that identifies the strings with the number in StringID?
ans=[3;
2;
3;
1;
3]
I would like this to be scaleable to a few thousand rows and a StringID column of a couple hundred strings. I feel like there is a simple solution I'm not thinking of. I assume there is some combo of find and strcmp that I'm not thinking of. I could probably just do it with a loop, but I am trying to avoid that.

採用された回答

Adam Danz
Adam Danz 2020 年 8 月 3 日
編集済み: Adam Danz 2020 年 8 月 3 日
StringID needs to be a cell array. Then,
[~, idx] = ismember(tc,StringID(:,1));
Also see lower() if you want to control for cases.
  2 件のコメント
madhan ravi
madhan ravi 2020 年 8 月 3 日
Ah much better ;)
Ryan Rose
Ryan Rose 2020 年 8 月 3 日
This is exactly what I was looking for. Thank you.

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

その他の回答 (1 件)

madhan ravi
madhan ravi 2020 年 8 月 3 日
T = cell2table(StringID(:, 2));
T.Properties.RowNames = StringID(:, 1);
Wanted = T{num2cell(tc), :}

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by