Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

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



  • Prev by Date: Imported Data in Mathematica Player or CDF Documents
  • Next by Date: Re: Find position of nonzero elements
  • Previous by thread: Re: Find position of nonzero elements
  • Next by thread: Re: Find position of nonzero elements