Create cell array of cell arrays from data file

4 visualizaciones (últimos 30 días)
Aram Schiffman
Aram Schiffman el 2 de Jun. de 2017
Comentada: Aram Schiffman el 5 de Jun. de 2017
Hi all, I guess this boils down to a syntax question, or maybe a method question. Let's say I have read in data (mix of text and numbers) from an Excel file, into a cell array. Let's say this data array -- call it dataCell -- has 5 rows and 4 columns.
A script has determined that rows 1, 2, and 4 have something in common. I want to group them. Similarly, the script has determined that rows 3 and 5 have something in common, so I want to group those too. In this example, the result needs to be two groups. Next time, there might be 1 group, or 50, or any other number of groups. It is not a fixed number.
I want to create a cell array of cell matrices. Each sub-array is a cell matrix that contains a group of data rows that belong together. So in my example, my cell array will contain two sub-arrays. The first will be a 3x4 cell matrix. The second will be a 2x4 cell matrix. Easy enough, right?
A cell array called rowMatches lists the grouped rows. It would look like this for the example: rowMatches = {[1;2;4];[3;5]} Make sense?
Now, how do I write rows 1, 2 and 4 to my first sub-matrix, and rows 3 and 5 to my second? It would be slick if there were a notation kinda like this (even slicker if I could eliminate the for loop):
groupedDataCell={};
for i=1:length(rowMatches)
groupedDataCell=[groupedDataCell;{dataCell{rowmatches{i,:}}}]
end
I probably botched the number of { and }, but you get the idea. If such a thing is possible, how?
Barring that, it's for loops... right? If I start with
groupedDataCell={{} {}}
then I know how to loop through the data and append each data row to the appropriate sub array. In other words, if I can start with
groupedDataCell={{} {}}
, I am fine from there on.
But I can't. I won't know how many sub-array (here two, maybe more next time). I would have thought this would work (or about a million variants I've tried):
groupedDataCell={{}};
then loop as many time as needed...
groupedDataCell=[groupedDataCell;{}]
to get this {{};{}} (I don't really care if it is vertical or not)
but MATLAB doesn't think that way. The concatenate function returns {{}} with every iteration.
So, question. How do I create a cell array of n empty cell arrays, where n can vary?
Second question. I created the empty arrays to write data to. Is there another method that would shortcut the need for the empty array (which on its own is useless)?
Thanks all!
Aram

Respuestas (2)

dpb
dpb el 3 de Jun. de 2017
Editada: dpb el 3 de Jun. de 2017
  1. "How do I create a cell array of n empty cell arrays, where n can vary?" C=cell(n,1);
  2. "Is there another method that would shortcut the need for the empty array (which on its own is useless)?"
If you have the V vector above mentioned previously determined, then just use it...
for i=1:length(V)
C{i}=dataCell(V{i},:);
end
  1 comentario
Guillaume
Guillaume el 3 de Jun. de 2017
Or with cellfun as a one liner (which may actually be slower than the explicit loop):
groupedDataCell = cellfun(@(rows) dataCell(rows, :), rowMatches, 'UniformOutput', false)

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 3 de Jun. de 2017
I think this does what you asked for. Hopefully it's also what you want:
dataCell = {1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16;17,18,19,20}
rowMatches = {[1;2;4];[3;5]}
numberOfGroups = length(rowMatches);
groupedDataCell = cell(numberOfGroups, 1)
for k = 1 : numberOfGroups
% Find out what rows we should extract for this group.
thisMatch = rowMatches{k};
groupedDataCell{k} = dataCell(thisMatch, :);
end
% groupedDataCell is a 2x1 cell array.
% In each cell is another cell.
% Print out final cell array
celldisp(groupedDataCell)
  1 comentario
Aram Schiffman
Aram Schiffman el 5 de Jun. de 2017
Thanks to both of you! I had tried using Matlab "cell" command but got hung up because my data array has text. Not sure what I did wrong before. Image Analyst, I used you code but with text instead of numbers as data, works perfectly. Thanks! Much appreciated.
Aram

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by