Re: Find position of nonzero elements

*To*: mathgroup at smc.vnet.net*Subject*: [mg120358] Re: Find position of nonzero elements*From*: Murray Eisenberg <murray at math.umass.edu>*Date*: Wed, 20 Jul 2011 06:31:08 -0400 (EDT)*References*: <201107191053.GAA10147@smc.vnet.net>*Reply-to*: murray at math.umass.edu

There must be a simpler way, but here's one way. mat = {{-2, -3, 0, -1}, {3, -2, 0, 2}, {0, -1, 1, 2}}; mn = Dimensions[mat]; ij = Table[{i, j}, {i, First@mn}, {j, Last@mn}]; Pick[Flatten[indices, 1], Thread[0 != Flatten@mat]] {{1,1}, {1,2}, {1,4}, {2,1}, {2,2}, {2,4}, {3,2}, {3,3}, {3,4}} The difficulty is that Mathematica, in contrast to some programming languages, really has no sense of higher-order arrays, just of 1-dimensional lists, so that it really has no such thing as a matrix, just a list of lists. On 7/19/11 6:53 AM, Dominic W=F6rner wrote: > Hi, > > How can I find the indices i and j of all nonzero elements of a matrix. > I want that because there are only some nonzero elements in a huge > matrix. > > Best regards, > Dominic > -- Murray Eisenberg murray at math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305

**References**:**Find position of nonzero elements***From:*Dominic Wörner <dominic.woerner@mpi-hd.mpg.de>