Question

I have a cell array containing 750 "documents" (750 arrays of words within the single cell array). I am trying to concatenate all of the words to make a single array. So far I know I need to use a for loop to iterate through each array and append to the end of the last one, however my code is giving me the wrong answer:

list = cell(1,1);
    for i = 1:length(docs)
   prevList = list;
    list = [prevList;docs{i}];
end

My thoughts are that my initialisation of list is incorrect as it produces:

    [1x1635 char]
    [1x1476 char]
    [1x531  char]
    [1x103  char]
    [1x1725 char]
    [1x344  char]
    [1x463  char]
    [1x739  char]
    [1x762  char]
    [1x1139 char]
    [1x89   char]
    [1x361  char]
    [1x334  char]
    [1x520  char]
    [1x219  char]
and so forth...

as opposed to a list of words.

If anyone could help me, I would very much appreciate it.

Was it helpful?

Solution

No for loop needed. Let's use a small example:

str1 = 'The quick brown ';
str2 = 'fox jumped over the ';
str3 = 'lazy dog. '
docs = {str1;str2;str3} % Your cell array containing arrays of text in each row

docs_cat = [docs{:}] % Concatenate

which returns:

docs_cat =

The quick brown fox jumped over the lazy dog. 

OTHER TIPS

I don't know if there's a quicker solution to append, but you should be able to do the following:

list = prevList;
prevLength = length(list);
for i=1:length(docs)
    list{prevLength+i} = docs{i}
end

It doesn't matter that 'prevLength+i' is out of range during assignment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top