Cell array to double array?

13 vues (au cours des 30 derniers jours)
L'O.G.
L'O.G. le 25 Août 2022
Commenté : Walter Roberson le 25 Août 2022
I have a cell array with each element a vector of type double. How do I convert this to an array of type double?

Réponses (1)

Walter Roberson
Walter Roberson le 25 Août 2022
cell2mat — but first you might need to reshape() the cell. Sometimes you need to first use cellfun an anonymous function that reshapes the individual cells. It depends what the original sizes are and the target size.
  2 commentaires
L'O.G.
L'O.G. le 25 Août 2022
I should've mentioned the vector in each element of the cell array is of a different length, so I imagine cell2mat wouldn't be easy to use for this.
Walter Roberson
Walter Roberson le 25 Août 2022
That is not necessarily a problem.
cell2mat does the equivalent of looping over the columns of the input cell and vertcat(CELL{:,COLUMN}) to produce numeric columns that it puts into cells. Then it loops over rows of that cell doing horzcat() of the expansion, producing 2d slices that it puts in cells. Then it loops over the 2d slices and stiches them together, and so on.
So for the first column of the input cell, it does not matter if the cell entries have different numbers of rows as long as they all have the same number of columns within each cell column. Then when it goes to put together the columns it only matters the total number of rows in each column added up.
For example, {1x2;3x2} is happy because the number of columns was consistent, producing a 4x2. If the second column of the cell input had been, {2x5;2x5} then columns consistent producing 4x5. And then in the second pass the 4x2 is row consistent with the 4x5 so 4x7 output.
Every entry of the cell input could be a different shape as long as the right dimension is consistent at the right time and the totals are consistent. You just have to be aware of the order everything is put together.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by