cell to matrix
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I have a data set which is in a cell structure (<1x20>) I want to place all of the data into one matrix. As each of the data sets in this cell structure is of different size, such as the first one is: 61169x15 and the second is 59529x15 and so on... i'm trying to find a way of combining the cells into a matrix. I can do this manually by:
new_data=[data1;data2;data3;data4]
But i would like to run the same script for other data sets therefore am looking for a way of doing this in a loop. Is that possible? So, I was thinking of a loop which ran though the cells i.e. for i=1:size(data,2) %=1:20
and then applying the same process as above in the loop, but im not sure on how I would go about doing this.
thanks
1 Commento
Jan
il 4 Nov 2011
Please by careful with the term "structure". It is often confused with "STRUCT" array.
Risposta accettata
Jan
il 4 Nov 2011
If all cell elements have teh same number of columns:
new_data = cat(1, data{:});
3 Commenti
Jan
il 4 Nov 2011
@ricco: This is not possible. If all the arrays in the cell have the same number of columns, they can be concatenated using CAT(1, C{:}) or CELL2MAT(C'). If this does not work for your problem, the number of columns must be different. Please check this again:
cellfun('size', data, 2)
Più risposte (2)
Fangjun Jiang
il 3 Nov 2011
If all cells have the same number of columns, would this help?
a{1}=rand(3,2);
a{2}=rand(4,2);
a{3}=rand(5,2);
cell2mat(a')
2 Commenti
Jan
il 4 Nov 2011
@ricco: No, the type of the cell elements does not matter - btw.: Fangjun's data are DOUBLEs also. But the dimensions must be matching. While "cell2mat(a')" works, "cell2mat(a)" must fail.
Ora Zyto
il 3 Nov 2011
From your description, it seems like all of your data sets have the same number of columns, and varying number of rows. In your for loop, append your current dataset to your existing matrix:
for i=1:length(data),
new_data=[new_data;data{i}]
end
Ora
3 Commenti
Jan
il 4 Nov 2011
@Ora Zyto: The loop let the array grow in each iteration. This is very inefficient.
Vedere anche
Categorie
Scopri di più su Data Type Conversion in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!