Is it possible to grow cell matrices???
    5 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
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 commentaire
  James Tursa
      
      
 le 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.
Réponse acceptée
Plus de réponses (1)
  Matt Tearle
    
 le 5 Oct 2012
        
      Modifié(e) : Matt Tearle
    
 le 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 commentaires
  Matt Tearle
    
 le 9 Oct 2012
				Which they will, given the code I wrote. This is actually (fractionally) more efficient than deleting scattered locations.
Voir également
Catégories
				En savoir plus sur Data Type Identification dans Help Center et File Exchange
			
	Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



