Cell array to double array?

19 views (last 30 days)
L'O.G.
L'O.G. on 25 Aug 2022
Commented: Walter Roberson on 25 Aug 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?

Answers (1)

Walter Roberson
Walter Roberson on 25 Aug 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 Comments
Walter Roberson
Walter Roberson on 25 Aug 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.

Sign in to comment.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by