Is it possible to grow cell matrices???

조회 수: 5 (최근 30일)
Nikolay Rodionov
Nikolay Rodionov 2012년 10월 5일
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
James Tursa
James Tursa 2012년 10월 5일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2012년 10월 5일
편집: Matt J 2012년 10월 6일
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) );

추가 답변 (1개)

Matt Tearle
Matt Tearle 2012년 10월 5일
편집: Matt Tearle 2012년 10월 5일
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
Matt Tearle
Matt Tearle 2012년 10월 9일
Which they will, given the code I wrote. This is actually (fractionally) more efficient than deleting scattered locations.
Matt J
Matt J 2012년 10월 9일
Ah. okay, i get it now.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by