Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Is it possible to grow cell matrices???

Asked by Nikolay Rodionov on 5 Oct 2012

Hi all,

Is it possible to grow cell matrices? I am writing a script that analyzes strings and saves them to a cell array if they meet a criteria. I do not know how many sequences will produce matches therefore I cannot assign an initial dimension size to my matrix such as finallarray = cell(X,Y).

I want to start out by creating a matrix with a single three cell row and then adding an additional row with each new match.

Alternatively, I could create a single really large matrix that I know will not be filled up completely and than scan it at the and find the first row index with empty cells, and then save all of the cells above it to a second array whose size is determined by the number of cells. However, I do not know how to a check for an empty cell. what is the code for null (matrix{3,1} == null)?

1 Comment

James Tursa on 5 Oct 2012

You can grow the array in a loop just like any other variable. However, each time you increase the size of the cell array the variable pointers it contains will need to be copied to a new memory block. How much this will slow down your code will depend on how many times you grow the array. Preallocating a large array first and then trimming it will mean the variable pointers will only have to be copied once.

Nikolay Rodionov

Products

2 Answers

Answer by Matt J on 5 Oct 2012
Edited by Matt J on 6 Oct 2012
Accepted answer

It's best to pre-allocate the cell with the maximum possible size and then post-delete the empty cell entries using

YourCell( cellfun('isempty', YourCell) );

Matt J
Answer by Matt Tearle on 5 Oct 2012
Edited by Matt Tearle on 5 Oct 2012

Cell arrays are essentially an array of pointers to other locations in memory where the actual data is held. Consequently, you need a contiguous block of memory for the cell array (the pointers), but the data inside each cell can be stored in its own block of memory. So, as James points out, how much of a performance hit you will suffer for growing the cell array depends on how big it is (not how big the contents are). From your description, it sounds like each cell contains only a single string (which is no big deal), so I'm guessing you're likely to have a large cell array (lots of individual strings). In that case, some kind of preallocation may help.

You say that you don't know how many strings will produce matches, but do you know how many strings you will test? If so, and you have the memory for it, you could use that as your initial size.

Given that you're adding elements sequentially as they pass the criterion, simply keep an index counter going and delete the unused cells at the end:

wordstotest = ...
maxn = length(wordstotest);
goodwords = cell(maxn,1);
idx = 1;
for k = 1:maxn
  if (<test wordstotest{k} here>)
    goodwords{idx} = wordstotest{k};
    idx = idx + 1;
  end
end
goodwords(idx:maxn) = [];

3 Comments

Matt J on 5 Oct 2012

Don't think so. Doing

    goodwords(idx:maxn) = [];

will always delete a consecutive range of goodwords cells, regardless of whether the empty cells occur consecutively.

Matt Tearle on 9 Oct 2012

Which they will, given the code I wrote. This is actually (fractionally) more efficient than deleting scattered locations.

Matt J on 9 Oct 2012

Ah. okay, i get it now.

Matt Tearle

Contact us