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.
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.
"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];
"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.
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.