Translating MATLAB's cell2mat into Mathematica

How I can define a function in Mathematica such that it acts same as the cell2mat command in MATLAB?

Example (in MATLAB):

c = {,    [2, 3, 4];
[5; 9], [6, 7, 8; 10, 11, 12]};
m = cell2mat(c)

%/* Output:
% m = 1     2     3     4
%     5     6     7     8
%     9     10    11    12
• You've been around this site for a year now. You voted only once, posted 5 questions, never accepted an answer and never answered a question. Is the site really useful for you? – Dr. belisarius Mar 29 '13 at 9:00
• Your example is directly taken from the Matlab documentation and is therefore not valid Mathematica code. What Mathematica code have you tried to solve this problem? – Matariki Mar 29 '13 at 9:11
• @BoLe I'm going to upvote your answer, but I think the question doesn't make any sense without context. Why should one drop down here a piece of syntactical sugar from any other language and ask for a "translation"? – Dr. belisarius Mar 29 '13 at 10:56
• I think this would be a fine question if a description of what the Matlab code were intended to do had been included.. – whuber Mar 29 '13 at 15:20
• K-1, rather than getting upset because people have down-voted the question, you could instead work to make it more clear. Many people many not be familiar with MATLAB syntax and I think this question would have been much better received if you had given a generic "pseudo-code" example and described the operation. Also, at the moment you are only -3 points as the up-votes count more than the down-votes. – Mr.Wizard Apr 6 '13 at 9:25

If you make all the component parts matrices, you can use ArrayFlatten

c = {{{{1}}, {{2, 3, 4}}}, {{{5}, {9}}, {{6, 7, 8}, {10, 11, 12}}}};
c // MatrixForm
ArrayFlatten[c] // MatrixForm  • I agree, this is the most natural way to do so. I was about to add this to my answer before I saw yours :) I hesitated at first, because the OP said BoLe's example is what they had in mind (but they probably didn't have a clue anyway, so it's better to show them the right way). My guess is that they got dinged badly in the votes because the lack of a clear example leads to different interpretations of what the original list should look like – rm -rf Mar 29 '13 at 14:52
• @rm-rf, I hesitated too, but figured that without an example it was my choice how to define the Mathematica "equivalent" of [2,3,4]. – Simon Woods Mar 29 '13 at 21:18
• Yeah, I hope the OP chooses this. This is certainly the smarter way to do it. – rm -rf Mar 29 '13 at 21:20
• @Simon Woods: I guess I would prefer this method as it suggests a general solution so for different matrices I don't need to check the dimensions all the time. – K-1 Mar 31 '13 at 10:30

There are already good explanations about how the restructuring of the nested arrays given as input to cell2mat can be done in Mathematica. I couldn't resist to mention the following, though:

The main purpose of the matlab function cell2mat is to convert from so called cell arrays to "normal" matlab matrices, where cell arrays correspond roughly to "normal" arrays (List) in Mathematica and normal matlab matrices correspond roughly to what Mathematica calls "packed arrays". The corresponding function in Mathematica would be DeveloperToPackedArray. Normal matlab matrices can -- just as Mathematica packed arrays -- only contain elements of exactly one data type and can only be rectangular. Thus the restructuring that cell2mat does in the OPs example more or less is just a side effect so that the conversion to a matrix will be possible at all. Unlike in Mathematica the use of cell arrays and matrices in matlab isn't interchangable, so it is often necessary to explicitly convert between the two, while in Mathematica the use of DeveloperToPackedArray is usually only used for performance optimizations. So one will see cell2mat and num2cell much more often in matlab code than DeveloperToPackedArray in Mathematica code just for that reason.

That said, one should be aware that cell2mat might indeed often be used for just such restructuring purposes in matlab code as it is a very convenient method to achieve that. It will only work if all elements are of the same type, though. This is the second reason why one probably sees cell2mat more often than DeveloperToPackedArray in Mathematica, which doesn't do any such restructuring.

• So far, the best answer was your explanation about what cell2mat really does! Using your comment I finally concluded that maybe a Flatten command works out in my code. Thank you so much. – K-1 Apr 5 '13 at 3:05
c = {{{1}, {2, 3, 4}},
{{{5}, {9}}, {{6, 7, 8}, {10, 11, 12}}}};

This works for this example, perhaps you can tweak it to your needs.

Switch[Depth[#], 3, Join @@ #,
4, Sequence @@ Join @@@ Transpose[#]] & /@ c
• You probably meant ... &/@c – Dr. belisarius Mar 29 '13 at 10:52

You can use the powerful second argument of Flatten (see this answer by Leonid for an explanation) to do most of the work and finally partition it to the correct size. I'll use BoLe's example too:

c = {{{1}, {2, 3, 4}}, {{{5}, {9}}, {{6, 7, 8}, {10, 11, 12}}}};

With[{m = Flatten[c, {{1}, {3, 2}}]},
Flatten@m ~Partition~ Length@Flatten@First@m
]

Building on the examples in BoLe's and Simon's answers:

c = {{{1}, {2, 3, 4}}, {{{5}, {9}}, {{6, 7, 8}, {10, 11, 12}}}};

ArrayFlatten @ Replace[c, x_?VectorQ :> {x}, {2}]

$\begin{array}{cccc} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \end{array}$