Deleting Zeros From a matrix

Home Forums Matlab Deleting Zeros From a matrix

This topic contains 1 reply, has 2 voices, and was last updated by  cK 9 years, 8 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #517

    vneelam
    Participant

    I have a double matrix[A,1,2] and A=[1:3028,1] I want to delete all the zeros in the matrix and make new matrix which contains only non-zero elements. Can some body help on this? Thanks in advance.

    Regards
    Neelam

    #518

    cK
    Keymaster

    In a vector, say with some (arbitrary) elements, A = [1,0,3,-1,2,0,9],
    zeros can be removed by linear indexing as A(A==0)=[];
    The resultant vector A will be shorter as elements equal to zero have been removed (in other words.. ‘replaced with empty elements’).
    However, if A is a matrix, [1 0; 3 -1; 0 9], zeros simply cannot be removed from the matrix, as it isn’t feasible, A turns into a row matrix with above operation. If you want to preserve the shape of A, then consider replacing 0s with NaNs or any other number that you may signify as invalid. E.g. A(A==0) = NaN;

    • This reply was modified 9 years, 8 months ago by  cK.
    • This reply was modified 9 years, 8 months ago by  cK.
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.