在matlab中创建一个给定字符串集和字典的整数矩阵

在matlab中创建一个给定字符串集和字典的整数矩阵,matlab,dictionary,matrix,Matlab,Dictionary,Matrix,我有一组字符串(作为Nx1矩阵)和一个字典(Mx1),其中包含字符串中的所有单词。 例如: 我想创建一个矩阵(大小为MxN),如果相应的单词出现在相应的tweet中,则单元格中包含1。我如何在matlab中实现它 我试着这样做: for i=1:len_of_dict for j=1:len_of_str temp=strfind(string1(j),dict(i)); x=find(cellfun(@isempty,temp));

我有一组字符串(作为Nx1矩阵)和一个字典(Mx1),其中包含字符串中的所有单词。 例如:

我想创建一个矩阵(大小为MxN),如果相应的单词出现在相应的tweet中,则单元格中包含1。我如何在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

但是我得到的向量是不正确的。求救

字符串应存储为单元格数组。然后,您可以使用
strsplit
中断tweets,然后使用
ismember
检查tweets中是否存在每个单词

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
将结果转换为矩阵:

out = vertcat(presence {:})
结果:

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

字符串应该存储为单元格数组。然后,您可以使用
strsplit
中断tweets,然后使用
ismember
检查tweets中是否存在每个单词

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
将结果转换为矩阵:

out = vertcat(presence {:})
结果:

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