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: [mg120370] Re: Find position of nonzero elements
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Wed, 20 Jul 2011 06:33:18 -0400 (EDT)

On 7/19/11 at 6:53 AM, dominic.woerner at mpi-hd.mpg.de (Dominic W=C3=B6rner)
wrote:

>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.

I will assume your matrix is not currently a SparseArray. If so,
do the following

Most[ArrayRules@SparseArray@m] /. HoldPattern[a_ -> _] -> a

where m is your matrix. ArrayRules applied to a SparseArray
returns a list of the form

{i,j}->x

where i,j are the indices of a non-zero entry and x is the value
of that entry. The last rule in this list will be {_,_}->0 which
is default rule for elements with a zero value you aren't
interested in. Finally, the replacement rule simply extracts the
{i,j} portion of each rule for a non-zero entry.



  • Prev by Date: Inverse of Interpolating Function?
  • Next by Date: Re: Unexpected Behavior: SetDelayed versus Set
  • Previous by thread: Re: Find position of nonzero elements
  • Next by thread: Using Plot3D; rotating the plot but, getting a different image after exporting.