Search code examples
arraysmatlabinverse

How to inverse a whole cell array


I have one cell array A= {<2x6 double>,<4x6 double>,<43x6 doubl>}. and now I want to calculate the inverse value of each matrix elements inside the cell array. I have written below code but It doesn't work.

C = cellfun(@inv, A, 'Un', false);

can you please guide me how can I write the proper code? e.g the element inside of the cell array is 2 and I want to show the inverse of this value like 1/2


Solution

  • inv is the matrix inverse, which will give you the error Matrix must be square, as it tries to invert the three matrices inside of A. You probably want element-wise division: 1./X

    C = cellfun(@(X) 1./X, A, 'Un', false);