Clear Filters
Clear Filters

append data at the end of cellarray?

1 view (last 30 days)
arun
arun on 29 Sep 2013
Answered: arun on 29 Sep 2013
  1. fid = fopen('TEXTFILE.txt','r');
  2. tline = fgets(fid);
  3. while ischar(tline)
  4. words=regexp(tline,'<s>|\w*|</s>','match');
  5. * trigram=cellfun(@(x,y,z) [x ' ' y ' ' z],words(1:end-2), words(2:end-1),words(3:end),'un',0); *
  6. tline = fgets(fid);
  7. end
  8. fclose(fid);
  9. uniquetrigram = unique(trigram);
  10. counts = cellfun(@(x)sum(strcmp(x,trigram)), uniquetrigram);
  11. % Group them together
  12. trigramcount = [uniquetrigram.' num2cell(counts.')]';
QUERY: I have problem in line number "5" because every time i want to store the output at the end of "trigram" cell array but it is storing the out of last line only. Every time i want to append the output of "* cellfun(@(x,y,z) [x ' ' y ' ' z],words(1:end-2), words(2:end-1),words(3:end),'un',0); *" in to "trigram" how can i do this?

Accepted Answer

arun
arun on 29 Sep 2013
I have solved this in following way. This is the solution i am using.
fid = fopen('Tiny_Corpus.txt','r');
tline = fgets(fid);
trigram={};
while ischar(tline)
words=regexp(tline,'|\w*|','match');
trigram1=cellfun(@(x,y,z) [x ' ' y ' ' z],words(1:end-2), words(2:end-1),words(3:end),'un',0);
trigram = [trigram trigram1];
tline = fgets(fid);
end
fclose(fid);
uniquetrigram = unique(trigram);
counts = cellfun(@(x)sum(strcmp(x,trigram)), uniquetrigram);
% Group them together
trigramcount = [uniquetrigram.' num2cell(counts.')]';

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!