Help with a function in a loop
    2 views (last 30 days)
  
       Show older comments
    
For example:
sampledata:
hello world
hello
hello world world
for i=1:size(sampleData,1) 
    [x y]=ngramsFreq(sampleData{i},n) 
end
% ngramsFreq is a function that returns the frequence and sequences of n letters
I want to caculate x and y for each sentence and store it that after the loop ends and get the specific data for each sentence, how could I do it? I thought bulding a matrix but the size of x and y for each sentence is not equal.
could you help me? Thank's!
0 Comments
Accepted Answer
  Andrei Bobrov
      
      
 on 17 Apr 2013
        function [s1, f] = ngramsfreq(str,n)
    n1 = numel(str) - n + 1;
    s1 = str(hankel(1:n1,n1:n1+n-1));
    [s1,~,c] = unique(s1,'rows','stable');
    f = accumarray(c,ones(n1,1));
end
using
str = {'hello world','hello','hello world world'};
n = 2;
nn = numel(str);
S = cell(nn,1);
F = S;
for jj = 1:nn
    [S{jj}, F{jj}] = ngramsfreq(str{jj},n);
end
More Answers (1)
  Yao Li
      
 on 17 Apr 2013
        sampledata={'hello world','hello','hello world world'};
length(sampledata)
for i=1:length(sampledata)
    for j=1:length(sampledata{i})
        [x{i} y{i}]=%add your function here
    end
end
4 Comments
See Also
Categories
				Find more on Data Type Conversion 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!