logic vector given two string arrays?

I am given 2 char arrays, let's call them A and B, A has constant dimensions and values and B varies in both.
I am trying to get a logic vector of all zeros and ones in a single column. for each row of the column I need the value to be one i the array in row one of A contains any of the strings in array B.
For example,
A=
'hello'
'world'
'this '
'is '
'an '
'examp'
'le '
and
B=
'is'
'he'
So the return would be
'1'
'0'
'1'
'1'
'0'
'0'
'0'
How would I write this to give it back in this form?

 採用された回答

Birdman
Birdman 2018 年 2 月 21 日

0 投票

A=["hello","world","this","is","an","examp","le"];
B=["is","he"];
contains(A,B)

6 件のコメント

Garrett Miller
Garrett Miller 2018 年 2 月 21 日
When I attempt to do that I get an error
Error using contains First argument must be a string array, character vector, or cell array of character vectors.
Birdman
Birdman 2018 年 2 月 21 日
Which version of MATLAB you have?
Garrett Miller
Garrett Miller 2018 年 2 月 21 日
R2017a
Birdman
Birdman 2018 年 2 月 21 日
contains function can be used for the versions after R2016b. Anyway, try this then:
A={'hello','world','this','is','an','examp','le'};
B={'is','he'};
contains(A,B)
Garrett Miller
Garrett Miller 2018 年 2 月 21 日
Okay that works much better, but how would I be able to set this up for B with varying amount of rows and columns?
Birdman
Birdman 2018 年 2 月 21 日
It should work for any size of B, just change B cell array according to your needs and run the code again. The size does not matter.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 2 月 21 日

0 投票

reshape( char('0' + ismember(cellstr(A), cellstr(B))), [], 1)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2018 年 2 月 21 日

回答済み:

2018 年 2 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by