0

I have matrix A of size 3 x 100 x 19.

 A(3x100x1) = 
Columns 1 through 4

0.0080    0.0090    0.0100    0.0110
0.1350    0.1350    0.1350    0.1350
1.7564    1.7564    1.7564    1.7582

Columns 5 through 8

0.0120    0.0130        0.0140     0
0.1350    0.1350        0.1350     0
1.7599    1.7599        1.7655     0 .... columns 100


 A(3x100x2) = 

 Columns 5 through 8

     0         0         0    0.0150
     0         0         0    0.1350
     0         0         0    1.7599... and so on till 100 column

Similarly till A(3x100x19)

I want to delete empty columns of 3D matrices and need A(3x10x1), A(3x10x2) an so on without empty columns.

I tried

    A(:,all(A == 0),:) = [] % it did not work.

I also tried

for m = 1:19
    B = A(:,:,segId)
    B(:,all(A == 0),:) = [];
end                               %% It did not work.

Can anyone help me with this?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Akash
  • 103
  • 1
  • 14
  • 1
    If you have lots of empty entries, you could have a look at sparse matrices: http://de.mathworks.com/help/matlab/ref/sparse.html – zinjaai Apr 20 '16 at 15:44

1 Answers1

3

You can't. Imagine this as a cube: you can't go about punching holes in it in MATLAB. You need a solid cube. Say I eliminate A(2,4,5), how is MATLAB supposed to show that? Swiss cheese is no option for MATLAB, so I'd leave the columns at zero, or, alternatively, set them to nan.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • Okay. Thanks. So , that means deleting empty rows or columns from the 3d matrix is not possible. – Akash Apr 20 '16 at 15:38
  • Indeed. You need to have your matrix to be a rectangle in 2D, a girder in 3D and their equivalents in higher dimensions. Since I have no idea why you want this, I can only suggest to either make your program ignore `0` values, or put in some fill value like `-9999` or `nan`. – Adriaan Apr 20 '16 at 15:41
  • Okay. How can I convert this matrix into sparse matrix? Because I don't think, sparse 3d matrix is possible.? @Adriaan – Akash Apr 20 '16 at 16:07
  • @Akash oh indeed. I forgot you can't actually do that. Probably because you need to store `N+1` numbers for an `N` dimensional matrix (i.e. its index in all dimensions + the actual value of the element). So it's limited to 2 dimensions max. Sorry for confusing you. – Adriaan Apr 20 '16 at 16:12
  • No problem. I have tried to use cell array. It work, but slow. If you can help with that? @ Adriaan. – Akash Apr 20 '16 at 16:19
  • @Akash a sparse matrix isn't going to help you here, your matrix is not sparse. – sco1 Apr 20 '16 at 17:23