Path: news.mathworks.com!not-for-mail
From: "Tim Davis" <davis@cise.ufl.edu>
Newsgroups: comp.soft-sys.matlab
Subject: Re: deleting 0s from an array
Date: Fri, 17 Aug 2007 09:19:25 +0000 (UTC)
Organization: University of Florida
Lines: 23
Message-ID: <fa3p6t$mn1$1@fred.mathworks.com>
References: <fa07po$f1c$1@fred.mathworks.com> <fa08s1$r4b$1@fred.mathworks.com> <1187326130.279914.104760@g12g2000prg.googlegroups.com> <fa3kot$4mp$1@fred.mathworks.com> <fa3n8c$1an$1@canopus.cc.umanitoba.ca>
Reply-To: "Tim Davis" <davis@cise.ufl.edu>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1187342365 23265 172.30.248.37 (17 Aug 2007 09:19:25 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 17 Aug 2007 09:19:25 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 45902
Xref: news.mathworks.com comp.soft-sys.matlab:424268


roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in m
> >> I can tie:
> >> d=d(~~d)

You did better than tie.  The above statement is twice as
fast as the other two alternatives.  I tried d of size 1e6
with 10% nonzeros.

All of these solutions, however, destroy the location
information of the nonzeros in the array.  Just as fast as
d=d(~~d), and cleaner in my mind, is the simple

d=sparse(d)

Then if you want the location information, use find(d).  You
can't use find(d) with the other solutions.

Do function calls count as one "character" in MATLAB golf? 
If so, I win :-).

Note, however, that if you want to subreference this array,
make sure that d is a row vector.  Otherwise d(i) where i is
a scalar, will be slow, if d is a column vector.