0

Is there any inbuilt function to display all the submatrices from a matrix in matlab? For displaying one submatrix we have the function x(:,m:n). For eg: Consider a 4 x 4 matrix

   A = 14    11    16    16
       15     2    16     8
        3     5     3    13
       15     9    16     3

If the size of all submatrices is 1 x 2 , how to display all the submatices present in A? Also how to represent each submatrix with a variable (for eg; a1,a2..etc)

   a{1}=[14 11]
   a{2}=[16 16]
   .
   .
   a{8}=[16 3] 

How to display all the submatrices in the form of a cell array? please help. thanks in advance

  • 1
    Don't use names like `a1`, `a2`, `a3`, etc. They are very hard to work with later! Try using a cell array: `a{1}=[14 11]`, `a{2}=[16 16]`, it will be much easier! – David Jan 22 '15 at 04:36

2 Answers2

1

You could use the mat2cell command to split the original matrix as desired, and then cell2mat to obtain the result as a (sub)matrix again. In your particular example, using the same matix A as above, you could use it this way:

B = mat2cell(A,[1 1 1 1],[2 2])

cell2mat(B(1,1))
ans =

   14   11

cell2mat(B(1,2))
ans =

   16   16

cell2mat(B(4,2))
ans =

   16    3
Jorge Torres
  • 1,426
  • 12
  • 21
1
  1. Reshape the matrix into a linear array
  2. Reshape the linear array into whatever format you want, which in this case is 2 by x matrix, so that each row is one sub-matrix.

    reshape(reshape(A', numel(A), 1), 2, numel(A)/2)'
    

If you want to cast the results into cells, you can use the command mat2cell().

rayryeng
  • 102,964
  • 22
  • 184
  • 193
wshan
  • 21
  • 2
  • 2
    This works, but some explanation on how this works, rather than simply code dumping may be beneficial. Also, the OP wants a cell array. This only produces a 2D matrix. – rayryeng Jan 22 '15 at 08:33