Convert Strings into vectors of binary numbers

I need to convert utterance strings into vectors where each dimension of the vector represents a unique word, while reading the script from an m.file. So for example if I have, out='hello' 'my' 'name' 'is' 'sam'. I want a vector with binary numbers where hello is true and everything else is false.
Solution would be output=[1,0,0,0] How can I do this? Here is what I have so far
fid=fopen('sw4325.m')
>> firstline=fgets(fid);
>> sprintf(firstline);
>> splitstring=textscan(ans, '%s');
>> out=splitstring{:};

 採用された回答

Jan
Jan 2013 年 1 月 25 日

1 投票

I do not get it. Do you want STRCMP?
str = {'hello' 'my' 'name' 'is' 'sam'};
match = strcmp('hello', str);

5 件のコメント

Sam
Sam 2013 年 1 月 25 日
I mean turn the strings into a vector of binary numbers so hello my name is sam would become [1,0,0,0,0] if hello is in the string. I have to do this for the whole file so, I'll get vectors with 1s and 0s where the 1s are for when the string 'hello' holds true
José-Luis
José-Luis 2013 年 1 月 25 日
Have you actually tried Jan's code? It does exactly what you ask for, as far as I understand it.
Sam
Sam 2013 年 1 月 25 日
Thank Jan, your code does work for strings. But I have my data in a cell of strings, and it won't work for it. Here is my code f=fopen(‘hello.m’) firstline=fgets(f) sprint(firstline) splitstring=textscan(ans, ‘%s’) out=splitstring{:}
Do I need to convert this into strings first? If so, how do I do that, I'm new to Matlab, and I usually use Java so it's a little confusing
Jan
Jan 2013 年 1 月 28 日
Sorry, I do not understand which data are strings and which are cell strings. Using ans directly is prone to errors. Better split the string explicitly, e.g. out = regexp(firstline, ' ', 'split'). And then please explain again the type of the inputs. Either strcmp or ismember should solve the problem.
Samira
Samira 2013 年 1 月 28 日
Yes it works, thank you so much.

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

その他の回答 (0 件)

カテゴリ

質問済み:

Sam
2013 年 1 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by