2

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.

5
  • What does doc{i} contain exactly? Is it a char vector, or a cell array? Can you give a small example for two or three documents with a few words each? Commented Feb 13, 2014 at 23:41
  • By the way, [1x1635 char] is just Matlab's way of shortening a string inside a cell that is too long to print out to the command window. If the line was fewer that 80 characters (I believe -or whatever your preference is set to) then the actual string would be shown. Commented Feb 13, 2014 at 23:44
  • doc{i} is a cell array. There is a paragraph of words in each cell, doc{1}, doc{2} and so on. @horchler from what you said, maybe that means my code was correct from the beginning? Commented Feb 13, 2014 at 23:49
  • @Sadeep: To be clear, what is class(doc{1})? If my answer works then it should be char. You may be confusing the elements of a cell array (indexed with {} brackets) with the cells themselves. Commented Feb 13, 2014 at 23:55
  • I'm not so sure about the answer to your question, however you have been great help. I will search more tomorrow as it is late here, so appreciate the help. Commented Feb 14, 2014 at 0:02

2 Answers 2

2

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. 
Sign up to request clarification or add additional context in comments.

4 Comments

It might take a while for OP to do this 750 times!
@David: The OP indicated that the cell array (docs in this example) already existed. So all that is needed is the last line. The rest just provides an example.
This makes sense cheers. If I wanted to have unique words only appearing, using unique(), how would this work?
@Sandeep: You're welcome. Please accept / vote up answers that you find helpful. I'm not sure what you asking with regards to unique. It seems like a separate question not related to this one. Perhaps your should formulate a new question specifically about that.
0

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.

1 Comment

I'm not too sure how that would work as prevList isn't initialised? I have been asked to do it using the var = {x;y} format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.