Help with a function in a loop

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!

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 17 de Abr. de 2013

0 votos

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

Más respuestas (1)

Yao Li
Yao Li el 17 de Abr. de 2013

0 votos

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 comentarios

googo
googo el 17 de Abr. de 2013
thank's but it doesn't work. I entred ngramsFreq(sampleData{i},n) where you told me.
Example of the function:
>> [ngrams freq]=ngramsFreq('abc abd',2) % n=2
ngrams =
ab
bc
c
a
bd
freq =
2
1
1
1
1
Now, I want to do this for each sentence in the sampledata and store it.
I'm very thankful for your help.
Yao Li
Yao Li el 17 de Abr. de 2013
Editada: Yao Li el 17 de Abr. de 2013
sorry, but I think the results are expected. So I wander what you actually want?
googo
googo el 17 de Abr. de 2013
maybe a simple question that whould help figure this out. when I'm writing [x{1} y{2}] , what does it mean?
googo
googo el 17 de Abr. de 2013
He explain how to store a series of vectors from a for loop but each vector has the same length. How could I store it when the lengths are diffrent?
I understand you use x{i} y{i} but when i'm typing x{1} [2 2 2] for example I get "Cell contents assignment to a non-cell array object." and don't understand the meaning.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 17 de Abr. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by