Using strcmp on multiple strings to get a logical array

215 ビュー (過去 30 日間)
Marc Cousoulis
Marc Cousoulis 2017 年 1 月 12 日
編集済み: James Tursa 2017 年 1 月 16 日
I want to check for the occurrence of 3 possible events in a series of events..
Event = ['a1','b1','a1','c1','b1']';
Check = {'a1','b1','c1'}';
LogA(:,1) = strcmp(Event,Check{1});
LogA(:,2) = strcmp(Event,Check{2});
LogA(:,3) = strcmp(Event,Check{3});
This gives me a 5x3 logical array, but is there a way to do it without calling each column individually? i.e., what if I had 100 strings I wanted to check for and the goal was to have a 5x100 logical array? I don't want to use a "for-loop". I'm assuming there is some matrix way to do this.
  1 件のコメント
Jan
Jan 2017 年 1 月 15 日
Note:
Event = ['a1','b1','a1','c1','b1']';
is the same as:
Event = ('a1b1a1c1b1').';
Most likely you mean a cell string instead.

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

採用された回答

Kirby Fears
Kirby Fears 2017 年 1 月 12 日
編集済み: Kirby Fears 2017 年 1 月 12 日
There's really nothing wrong with a for loop in this case. However, cellfun is probably what you're looking for:
Event = {'a1','b1','a1','c1','b1'}';
Check = {'a1','b1','c1'}';
l = cellfun(@(c)strcmp(c,Event),Check,'UniformOutput',false);
This produces a cell array the size of Check with logical arrays the size of Event.
You can index like this:
l{index1}(index2)
  2 件のコメント
Stephen23
Stephen23 2017 年 1 月 13 日
編集済み: Stephen23 2017 年 1 月 13 日
Also simple to convert to a logical array:
>> Event = {'a1','b1','a1','c1','b1'};
>> Check = {'a1','b1','c1'};
>> X = cellfun(@(c)strcmp(c,Event),Check,'UniformOutput',false);
>> vertcat(X{:})
ans =
1 0 1 0 0
0 1 0 0 1
0 0 0 1 0
Jan
Jan 2017 年 1 月 15 日
編集済み: James Tursa 2017 年 1 月 16 日
strcmp does not accept: strcmp(Event, Check.') and this does not work also:
bsxfun(@strcmp, Event, Check.') % ERROR!
What a pity.

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

その他の回答 (2 件)

Guillaume
Guillaume 2017 年 1 月 13 日
編集済み: Guillaume 2017 年 1 月 13 日
Another option which may or may not be faster than cellfun(...):
Event = {'a1','b1','a1','c1','b1'}';
Check = {'a1','b1','c1'}';
LogA = false(numel(Event), numel(Check));
[~, loc] = ismember(Event, Check);
LogA(sub2ind(size(LogA), 1:numel(Event), loc.')) = true

John BG
John BG 2017 年 1 月 13 日
Marc
1.
If all 2nd halves of the strings contained in Event is '1', it is reasonable to ignore the entire column:
Event = ['a1';'b1';'a1';'c1';'b1'];
Event_num=double(Event)
=
97 49
98 49
97 49
99 49
98 49
E=Event_num(:,1)';
E =
97 98 97 99 98
2.
Same applies to Checks
Check = ['a1';'b1';'c1'];
Check_num=double(Check)
=
97 49
98 49
99 49
C=Check_num(:,1)';
C =
97 98 99
3.
Occurence of events are listed in variable occ
[Lie,occ]=ismember(E,C)
Lie =
1 1 1 1 1
occ =
1 2 1 3 2
4.
check
Check(occ)
=
abacb
meaning occ contains the occurrences: a1 b1 a1 c1 b1
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG
  5 件のコメント
Guillaume
Guillaume 2017 年 1 月 14 日
As is clearly explained in the help page, answers can only be accepted by somebody else after 7 days of inactivity by the original author.
Jan
Jan 2017 年 1 月 15 日
編集済み: Jan 2017 年 1 月 16 日
@John BG: Marc can still decide, even if he knows why your solution in not efficient. Other readers and you can profit from this information also.

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

カテゴリ

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