Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #3847

Re: Find position of nonzero elements

From Bill Rowe <readnews@sbcglobal.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Find position of nonzero elements
Date 2011-07-20 10:38 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <j06b77$nln$1@smc.vnet.net> (permalink)

Show all headers | View raw


On 7/19/11 at 6:53 AM, dominic.woerner@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.

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Find position of nonzero elements Bill Rowe <readnews@sbcglobal.net> - 2011-07-20 10:38 +0000

csiph-web