Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: deleting 0s from an array

Subject: deleting 0s from an array

From: James

Date: 16 Aug, 2007 01:03:52

Message: 1 of 8

  Is there any easy way to delete all zero values from an
array (i.e., other than going through each element and
rebuilding)?

  This is a 1-D array whose values are used in a
histogram. Sometimes the bins are too wide in value to get
much information, so I wanted to narrow down by looking at
individual bins with more granularity. I can find the
maximum value range for the bins, then the next highest,
etc. and generate histograms.

   Or is there a better idea?

Thanks




Subject: Re: deleting 0s from an array

From: us

Date: 16 Aug, 2007 01:22:09

Message: 2 of 8

James:
<SNIP no-zeros, please, evergreen...

> Is there any easy way to delete all zero values from an
array...

one of the copious solutions

% the data
     d=[0,0,0,1,0,0,0,2,0,0,0,3]
% ...without zeros
     d(d==0)=[]

us

Subject: Re: deleting 0s from an array

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 16 Aug, 2007 02:42:24

Message: 3 of 8

In article <fa08s1$r4b$1@fred.mathworks.com>, us <us@neurol.unizh.ch> wrote:
>James:
><SNIP no-zeros, please, evergreen...

>> Is there any easy way to delete all zero values from an
>array...

>one of the copious solutions

>% the data
> d=[0,0,0,1,0,0,0,2,0,0,0,3]
>% ...without zeros
> d(d==0)=[]

I can "golf" that!

d(~d)=[] %provided d does not contain a nan
--
  If you lie to the compiler, it will get its revenge. -- Henry Spencer

Subject: Re: deleting 0s from an array

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 17 Aug, 2007 08:46:04

Message: 4 of 8

In article <fa3kot$4mp$1@fred.mathworks.com>, us <us@neurol.unizh.ch> wrote:
>Richard Brown:
><SNIP pseudo-golf...

>> > d(~d)=[] %provided d does not contain a nan

>> I can tie:
>> d=d(~~d)

>again, and as <walter roberson> pointed out: as long as <d>
>is finite... now, to test this would (probably) cost you an
>extra few chars...

d(~d)=[] and d=d(~~d) work for inf and -inf but neither works for NaN.

--
   Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson

Subject: Re: deleting 0s from an array

From: us

Date: 17 Aug, 2007 09:07:33

Message: 5 of 8

Walter Roberson:
<SNIP stumbled over <us>'s <finite>...

> d(~d)=[] and d=d(~~d) work for inf and -inf but neither
works for NaN...

correct! hasty use of the word <finite>...

us

Subject: Re: deleting 0s from an array

From: Tim Davis

Date: 17 Aug, 2007 09:19:25

Message: 6 of 8

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.

Subject: Re: deleting 0s from an array

From: Steven Lord

Date: 17 Aug, 2007 13:43:57

Message: 7 of 8


"Walter Roberson" <roberson@ibd.nrc-cnrc.gc.ca> wrote in message
news:fa3n8c$1an$1@canopus.cc.umanitoba.ca...
> In article <fa3kot$4mp$1@fred.mathworks.com>, us <us@neurol.unizh.ch>
> wrote:
>>Richard Brown:
>><SNIP pseudo-golf...
>
>>> > d(~d)=[] %provided d does not contain a nan
>
>>> I can tie:
>>> d=d(~~d)
>
>>again, and as <walter roberson> pointed out: as long as <d>
>>is finite... now, to test this would (probably) cost you an
>>extra few chars...


Add one more character and you get a version that works for inf, -inf, NaN,
and finite numbers.

> d(~d)=[] and d=d(~~d) work for inf and -inf but neither works for NaN.
>
> --
> Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson

% This line doesn't count in the score; it generates sample data to test the
solution
d = [0 1 2 -5 0 324.65 Inf 0 -Inf NaN 0];

% Hole in 9
d=d(d~=0)

--
Steve Lord
slord@mathworks.com


Subject: Re: deleting 0s from an array

From: Tim Davis

Date: 17 Aug, 2007 16:14:22

Message: 8 of 8

"Tim Davis" <davis@cise.ufl.edu> wrote in message
> Then if you want the location information, use find(d). You
> can't use find(d) with the other solutions.
>

Umm. I guess my brain is too sparse. I should have
mentioned that to use find(d) you would do

[i j x] = find(d)

then x is the vector you're looking for, with zeros deleted,
and i is the list of d(i)'s that are nonzero (assuming d is
a column vector).

The above works if d is full or sparse; you don't have to do

d = sparse (d)

first. I think the above is really the simplest way to do
it, personally. But then I'm used to seeing "find"
statements flying around in m-files.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
evergreen us 30 Jan, 2008 20:32:29
sparse Patrick HannaSecure 22 Aug, 2007 15:48:07
sparse Tim Davis 17 Aug, 2007 05:19:38
golf Matthew Simoneau 16 Aug, 2007 12:30:56
code us 15 Aug, 2007 21:25:05
index us 15 Aug, 2007 21:25:05
logical indexing us 15 Aug, 2007 21:25:05
delete values James 15 Aug, 2007 21:05:07
arrays James 15 Aug, 2007 21:05:07
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics