Extract square matrices from a Nx2 matrix

Refresh

April 2019

Views

27 time

1

I'm working on matlab 2014, I have a matrix 5124x2 and I want to extract all the 2562 square matrices. I found that the unique solution is to use mat2Cell, but it doesn't work for me. I don't really need a cell array at the end, what I want is just all the square matrices

%example of data
A = rand(5124,2);
C = mat2cell(A,2,2*ones(2562,1));

I get the following error :

Error using mat2cell (line 106)
Input arguments, D1 through D2, must sum to each dimension of the input matrix size, [5124     2].'

Can you help me please ? Thanks

1 answers

4

To fix your code, it should be:

C = mat2cell(A,2*ones(2562,1));

And to reshape A into a 3D matrix of 2x2 slices, you can use:

C = permute(reshape(A.',2,2,[]), [2,1,3]);