フィルターのクリア

creating a feature matrix

4 ビュー (過去 30 日間)
Mahmoud Zeydabadinezhad
Mahmoud Zeydabadinezhad 2017 年 10 月 29 日
編集済み: Jos (10584) 2017 年 10 月 30 日
Hi, I have a cell array P of size 5000x1 which each element of it is another cell array of variable size (1xDIM). There is another cell array, vocab, of size 1x2000. The task is to create a matrix X of size 5000x2000 where X(i,j)=1 if vocab{1,j} is in P{i,1}. (P{i,1} is a cell array itself, not a single cell). I did this but the result is not correct:
for n=1:length(P)
for m=1:length(vocab)
if (strcmp(P{n},vocab{m}))
X(n,m) = 1;
else
X(n,m) = 0;
end
end
end
Thanks,

回答 (1 件)

Jos (10584)
Jos (10584) 2017 年 10 月 29 日
編集済み: Jos (10584) 2017 年 10 月 30 日
I think this should work:
vocab = {'A','B','C','D'} % 1-by-M cell array of strings
P = {{'A'} ; {'C','A'} ; {'B','A'}} % N-by-1 cell array of cell array of strings
X = arrayfun(@(k) ismember(vocab, P{k,1}), 1:size(P,1), 'un', 0) ;
X = cat(1,X{:}) % a N-by-M logical array

カテゴリ

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