Search code examples
matlabfor-loopconcatenationreshapecell-array

How to horizontally concatenate matrices present inside each cell of a cell array?


I have a 1x8 Cell Array(A) with elements composed of the following dimension.

10x13x2 double  10x13x2 double  10x13x2 double  10x13x2 double  10x13x2 double  10x13x2 double  10x13x2 double  10x13x2 double

where in A{1,1}....A{1,8} i have,

val(:,:,1) = 10x13 matrix(130 values)
val(:,:,2) = Another 10x13 Matrix(130 values)

Now i want to horizontally concatenate val(:,:,1) and val(:,:,2) so that i get a total of 260 values as a result of concatenation in each cell. i need to concatenate it like say if, val(:,:,1) is M and val(:,:2) is V. so i need to concatenate horizontally like [M1 V1]..[MN VN]

And i want this to be done for all the 8 elements in the cell array.

So, finally i have to get an value of a 1x8 cell array like the below

MxNX1 MxNX1 MxNX1 MxNX1 MxNX1 MxNX1 MxNx1 MxNX1 

where MXN = 260,

so finally i should get a 1x8 Cell array ,and i hope the dimension of each cell would be 260x8.

Kindly suggest how to do this, if it involves reshaping and loops kindly suggest me how to do this?


Solution

  • (:,:) notation for a 3D array horizontally concatenates the 3D slices. Use cellfun or a loop to apply that to all cells of A.

    A = cellfun(@(x) {x(:,:)}, A);
    

    Alternately, if the above version is less clear, you can use reshape.

    A = cellfun(@(x) {reshape(x,10,[])}, A);