Is there a way to stack multiple 2x2 matrices in MATLAB into a multidimensional array in a simpler way (e.g. without using "cat" or "reshape")?

Viewed 218

I am receiving a text file with 1000 matrices of size 2x2 each day from someone, in the following format (only 3 matrices are shown here instead of 1000):

0.96875000 0.03125000 
0.03125000 0.96875000

0.96875000 0.01562500 
0.03125000 0.98437500

0.99218800 0.03125000 
0.00781250 0.96875000

I need to make a 2x2x1000 array in MATLAB. Ideally I could do something simple like:

[0.96875000 0.03125000 
0.03125000 0.96875000;

0.96875000 0.01562500 
0.03125000 0.98437500;

0.99218800 0.03125000 
0.00781250 0.96875000]

After reading the MATLAB documentation on multidimensional arrays and the MATLAB documentation for the cat function, I figured out that I could make the required array in the following way (the first argument of cat is 3 because I'm concatenating the 2x2 matrices along the 3rd dimension):

cat(3,...
[0.96875000 0.03125000 
0.03125000 0.96875000],...
[0.96875000 0.01562500 
0.03125000 0.98437500],...
[0.99218800 0.03125000 
0.00781250 0.96875000])

But that does not work if I put spacing between the lines as in my "ideal" example above, and the need for all the commas and dots makes it a bit uglier in my opinion.

While writing this question, I have discovered that I can run my "ideal" example and then use reshape, which I prefer over my solution using the cat function. For this, I don't even need the semi-colons. However Cris Luengo correctly pointed out in the comments that reshape is not enough and permute is also needed, and then Luis Mendo pointed out in chat that the solution is not so simple:

permute(reshape(ideal.',2,2,[]),[2 1 3])

Andras Deak has done what we thought was impossible, which is to remove the transpose, but the solution is still quite complicated, and was not easy to engineer:

permute(reshape(ideal,2,[],2),[1 3 2])

Ideally one would not need to use cat or reshape to make a 3D array, when the original data is already so nicely formatted in what the human eye can already see is a 3D array of several 2x2 matrices.

Is there a simpler way to build the 3D array in MATLAB using the data in the format I have?

So far I have done the following on my own:

  • Searched online and found the above two MATLAB documentation articles which lead me to the above solution using cat
  • Came up with the above solution using reshape while writing this question, then it got improved by Cris and Luis in the comments and chat .
  • Also: I tried saving the data in a .txt file and clicked import in MATLAB, knowing that the import GUI gives some options for how the data is to be organized in the resulting MATLAB array, but there did not seem to be any option to make this a 3D array.
1 Answers

Indeed there is no "direct" way to import this text as a 3D matrix. This is the easiest way I can come up with:

  1. Save the input as a .txt file
  2. Use the import tool (Import Data button in the Variable toolbar) to import the data as a Mx2 matrix. Choose "Numeric Matrix" as "Output Type". And you can "exclude rows with" "blank cells" to avoid the empty rows.
  3. Besides reshape() and permute(), using cell array to format it as below might be more intuitive and less error prone to someones.
    % The number of 2x2 matrices
    N = size(m,1)/2;
    % Split each 2x2 matrix into a cell
    c = mat2cell(m, 2*ones(1,N), 1);
    % Concatenate along the 3rd dimension
    output3DMatrix = cat(3, c{:});
Related