Compare multiple columns in a cell array with strcmp

7 ビュー (過去 30 日間)
Dave
Dave 2016 年 2 月 2 日
編集済み: Stephen23 2016 年 2 月 2 日
I have an m x n cell array. I would like to pull specific rows that match strings from different columns. Currently, I'm accomplishing this with two matlab lines as follows:
hill_log = runlog1(strcmp(runlog1(:,4), 'Hill'),:);
hill_log2 = hill_log(strcmp(hill_log(:,6), 'Kilometer'),:):
I'd like to be able to do this on one line with one strcmp, possible?
Thanks Dave
  3 件のコメント
Guillaume
Guillaume 2016 年 2 月 2 日
What will make the code 100% clearer is a comment that states the intent.
Otherwise, I do believe that a one-liner using logical operations express the intent better that you want all runlog1 where column 4 is Hill AND column 6 is Kilometer.
Stephen23
Stephen23 2016 年 2 月 2 日
編集済み: Stephen23 2016 年 2 月 2 日
It is all a matter of taste of course, I would do something like this:
ishi = strcmp(runlog1(:,4), 'Hill');
iskm = strcmp(runlog1(:,6), 'Kilometer'):
hill_log2 = runlog1(ishi & iskm, :);

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

採用された回答

Guillaume
Guillaume 2016 年 2 月 2 日
Hum, why not use logical operations?
hill_log2 = runlog1(strcmp(runlog1(:,4), 'Hill') & strcmp(hill_log(:,6), 'Kilometer'), :);
In my opinion, it expresses the intent better than concatenating strings with a special character.

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 2 月 2 日
hill_log2 = runlog1( ismember( strcat(runlog1(:,4), char(65535), runlog1(:,6)), ['Hill' char(65535) 'Kilometer']), :);
You could use strcmp() instead of ismember() if you prefer.
char(65535) can be any character that you are sure does not appear in the columns. Except maybe char(0), that one is touchy, some of the routines malfunction on char(0)

カテゴリ

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