2016-11-27 7 views
1

文字列に含まれるすべての単語を含む一連の文字列(Nx1行列として)と辞書(Mx1)を持っています。 例:matlabで文字列と辞書のセットを与えられた整数の行列を作成します

strings= ['i went to the mall'; 'i am hungry'] 
dictionary = ['i','went','to','the', 'mall','am','hungry'] 

私は、対応する単語が対応するつぶやきに存在する場合、セルは1が含まれています(サイズM×Nのの)マトリックスを作成したいです。どのように私はmatlabでそれを行うことができますか?

は、私はこれをやってみました:

for i=1:len_of_dict 
    for j=1:len_of_str 
     temp=strfind(string1(j),dict(i)); 
     x=find(cellfun(@isempty,temp)); 
     xx = isempty(x); 
     if(xx~=0) 
      vec(i,j)=1; 
     end  
    end 
end 

しかし、私が得たベクトルが正しくありません。喜んで助けてください!

答えて

0

文字列をセル配列として格納することができます。 strsplitを使ってつぶやきを壊してからを使ってツイートの各単語の存在を確認できます。

out = vertcat(presence {:}) 

結果:

1 1 1 1 1 0 0 
1 0 0 0 0 1 1 

strings= {'i went to the mall'; 'i am hungry'}; 
dictionary = {'i','went','to','the', 'mall','am','hungry'}; 

splitted_strings = cellfun(@(x) strsplit(x,' '), strings, 'UniformOutput', false); 

presence = cellfun(@(s) ismember(dictionary, s), splitted_strings, 'UniformOutput', false); 

あなたは行列に結果を変換するvertcatを使用することができます

関連する問題