Search code examples
matlabmatrixcell

Using "cellfun" function to reshape a matrix in MATLAB


Here is a matrix on the left which contains with 2 cell members. Each member is a 3 x 2 matrix which include 3 cell member at its first column and 3 array matrix at the second column.

Please take a look at the picture:

enter image description here

Those "x" can be any thing. However, am trying to pick only shown numbers and organise them as it shown in the right matrix.

Could you please help me with that?


Solution

  • Where C is your cell matrix, a solution using nested cellfun:

    cellfun(@(x)(cellfun(@(y)(y(2)),x(:,2))), C, 'Uni', false)
    

    Maybe it's better to break it up a bit?

       f1 = @(x)cellfun(f2, x(:,2))
       f2 = @(y)(y(2))
    
       cellfun(f1, C, 'Uni', false)