Creating a Word Index Structure Array

6 ビュー (過去 30 日間)
Rick
Rick 2014 年 7 月 4 日
コメント済み: Image Analyst 2014 年 7 月 4 日
I am working on problem 2 in the attached PDF. When I run this right now, I get an error in my first conditional for if the word is not in the index. How do I say that the word does not exist in the index? Any other problems?
function Index = InsertDoc(Index, newDoc, DocNum)
IndexWords = {Index.Word};
for i = 1:numel(newDoc)
% If word is not in the Index
if isempty(Index) || Index(i).Words ~= IndexWords
Index(numel(IndexWords)+1).Word = newDoc{i};
Index(numel(IndexWords)+1).Documents = DocNum;
Index(numel(IndexWords)+1).Locations{end+1} = i;
else
end
% If the word does exist in the Index, but the occurance is unknown
% 1st occurance
if Index(i).Documents == DocNum
WordInIndex = strcmpi(newDoc{i},{Index.Word});
Index(WordInIndex).Documents = [Index(WordInIndex).Documents,DocNum];
Index(WordInIndex).Locations{end}(end+1)=i
% 2nd occurance or later
else
Index(WordInIndex.Locations{numel(Index(WordInIndex).Locations)+1}) = i;
end
end

回答 (1 件)

Image Analyst
Image Analyst 2014 年 7 月 4 日
You didn't attach anything. But you might look at the ismember() and isempty() functions.
  2 件のコメント
Rick
Rick 2014 年 7 月 4 日
My bad, I attached it now
Image Analyst
Image Analyst 2014 年 7 月 4 日
I don't really know what you want to do. Do you think you can use either of those two functions?

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

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by