<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154622</link>
    <title>MATLAB Central Newsreader - deleting 0s from an array</title>
    <description>Feed for thread: deleting 0s from an array</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 17 Aug 2007 16:14:22 -0400</pubDate>
      <title>Re: deleting 0s from an array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154622#388153</link>
      <author>Tim Davis</author>
      <description>"Tim Davis" &amp;lt;davis@cise.ufl.edu&amp;gt; wrote in message &lt;br&gt;
&amp;gt; Then if you want the location information, use find(d).  You&lt;br&gt;
&amp;gt; can't use find(d) with the other solutions.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Umm.  I guess my brain is too sparse.  I should have&lt;br&gt;
mentioned that to use find(d) you would do&lt;br&gt;
&lt;br&gt;
[i j x] = find(d)&lt;br&gt;
&lt;br&gt;
then x is the vector you're looking for, with zeros deleted,&lt;br&gt;
and i is the list of d(i)'s that are nonzero (assuming d is&lt;br&gt;
a column vector).&lt;br&gt;
&lt;br&gt;
The above works if d is full or sparse; you don't have to do&lt;br&gt;
&lt;br&gt;
d = sparse (d) &lt;br&gt;
&lt;br&gt;
first.  I think the above is really the simplest way to do&lt;br&gt;
it, personally.  But then I'm used to seeing "find"&lt;br&gt;
statements flying around in m-files.&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 17 Aug 2007 13:43:57 -0400</pubDate>
      <title>Re: deleting 0s from an array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154622#388130</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
"Walter Roberson" &amp;lt;roberson@ibd.nrc-cnrc.gc.ca&amp;gt; wrote in message &lt;br&gt;
news:fa3n8c$1an$1@canopus.cc.umanitoba.ca...&lt;br&gt;
&amp;gt; In article &amp;lt;fa3kot$4mp$1@fred.mathworks.com&amp;gt;, us  &amp;lt;us@neurol.unizh.ch&amp;gt; &lt;br&gt;
&amp;gt; wrote:&lt;br&gt;
&amp;gt;&amp;gt;Richard Brown:&lt;br&gt;
&amp;gt;&amp;gt;&amp;lt;SNIP pseudo-golf...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; &amp;gt; d(~d)=[]    %provided d does not contain a nan&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; I can tie:&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; d=d(~~d)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt;again, and as &amp;lt;walter roberson&amp;gt; pointed out: as long as &amp;lt;d&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt;is finite... now, to test this would (probably) cost you an&lt;br&gt;
&amp;gt;&amp;gt;extra few chars...&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Add one more character and you get a version that works for inf, -inf, NaN, &lt;br&gt;
and finite numbers.&lt;br&gt;
&lt;br&gt;
&amp;gt; d(~d)=[] and d=d(~~d) work for inf and -inf but neither works for NaN.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; -- &lt;br&gt;
&amp;gt;   Okay, buzzwords only. Two syllables, tops.  -- Laurie Anderson&lt;br&gt;
&lt;br&gt;
% This line doesn't count in the score; it generates sample data to test the &lt;br&gt;
solution&lt;br&gt;
d = [0 1 2 -5 0 324.65 Inf 0 -Inf NaN 0];&lt;br&gt;
&lt;br&gt;
% Hole in 9&lt;br&gt;
d=d(d~=0)&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 17 Aug 2007 09:19:25 -0400</pubDate>
      <title>Re: deleting 0s from an array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154622#388098</link>
      <author>Tim Davis</author>
      <description>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in m&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; I can tie:&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; d=d(~~d)&lt;br&gt;
&lt;br&gt;
You did better than tie.  The above statement is twice as&lt;br&gt;
fast as the other two alternatives.  I tried d of size 1e6&lt;br&gt;
with 10% nonzeros.&lt;br&gt;
&lt;br&gt;
All of these solutions, however, destroy the location&lt;br&gt;
information of the nonzeros in the array.  Just as fast as&lt;br&gt;
d=d(~~d), and cleaner in my mind, is the simple&lt;br&gt;
&lt;br&gt;
d=sparse(d)&lt;br&gt;
&lt;br&gt;
Then if you want the location information, use find(d).  You&lt;br&gt;
can't use find(d) with the other solutions.&lt;br&gt;
&lt;br&gt;
Do function calls count as one "character" in MATLAB golf? &lt;br&gt;
If so, I win :-).&lt;br&gt;
&lt;br&gt;
Note, however, that if you want to subreference this array,&lt;br&gt;
make sure that d is a row vector.  Otherwise d(i) where i is&lt;br&gt;
a scalar, will be slow, if d is a column vector.&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 17 Aug 2007 09:07:33 -0400</pubDate>
      <title>Re: deleting 0s from an array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154622#388093</link>
      <author>us</author>
      <description>Walter Roberson:&lt;br&gt;
&amp;lt;SNIP stumbled over &amp;lt;us&amp;gt;'s &amp;lt;finite&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; d(~d)=[] and d=d(~~d) work for inf and -inf but neither &lt;br&gt;
works for NaN...&lt;br&gt;
&lt;br&gt;
correct! hasty use of the word &amp;lt;finite&amp;gt;...&lt;br&gt;
&lt;br&gt;
us&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 17 Aug 2007 08:46:04 -0400</pubDate>
      <title>Re: deleting 0s from an array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154622#388085</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;fa3kot$4mp$1@fred.mathworks.com&amp;gt;, us  &amp;lt;us@neurol.unizh.ch&amp;gt; wrote:&lt;br&gt;
&amp;gt;Richard Brown:&lt;br&gt;
&amp;gt;&amp;lt;SNIP pseudo-golf...&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; &amp;gt; d(~d)=[]    %provided d does not contain a nan&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; I can tie:&lt;br&gt;
&amp;gt;&amp;gt; d=d(~~d)&lt;br&gt;
&lt;br&gt;
&amp;gt;again, and as &amp;lt;walter roberson&amp;gt; pointed out: as long as &amp;lt;d&amp;gt; &lt;br&gt;
&amp;gt;is finite... now, to test this would (probably) cost you an &lt;br&gt;
&amp;gt;extra few chars...&lt;br&gt;
&lt;br&gt;
d(~d)=[] and d=d(~~d) work for inf and -inf but neither works for NaN.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;Okay, buzzwords only. Two syllables, tops.  -- Laurie Anderson&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 16 Aug 2007 02:42:24 -0400</pubDate>
      <title>Re: deleting 0s from an array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154622#387919</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;fa08s1$r4b$1@fred.mathworks.com&amp;gt;, us  &amp;lt;us@neurol.unizh.ch&amp;gt; wrote:&lt;br&gt;
&amp;gt;James:&lt;br&gt;
&amp;gt;&amp;lt;SNIP no-zeros, please, evergreen...&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt;   Is there any easy way to delete all zero values from an &lt;br&gt;
&amp;gt;array...&lt;br&gt;
&lt;br&gt;
&amp;gt;one of the copious solutions&lt;br&gt;
&lt;br&gt;
&amp;gt;% the data&lt;br&gt;
&amp;gt;     d=[0,0,0,1,0,0,0,2,0,0,0,3]&lt;br&gt;
&amp;gt;% ...without zeros&lt;br&gt;
&amp;gt;     d(d==0)=[]&lt;br&gt;
&lt;br&gt;
I can "golf" that!&lt;br&gt;
&lt;br&gt;
d(~d)=[]    %provided d does not contain a nan&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;If you lie to the compiler, it will get its revenge. -- Henry Spencer&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 16 Aug 2007 01:22:09 -0400</pubDate>
      <title>Re: deleting 0s from an array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154622#387913</link>
      <author>us</author>
      <description>James:&lt;br&gt;
&amp;lt;SNIP no-zeros, please, evergreen...&lt;br&gt;
&lt;br&gt;
&amp;gt;   Is there any easy way to delete all zero values from an &lt;br&gt;
array...&lt;br&gt;
&lt;br&gt;
one of the copious solutions&lt;br&gt;
&lt;br&gt;
% the data&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;d=[0,0,0,1,0,0,0,2,0,0,0,3]&lt;br&gt;
% ...without zeros&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;d(d==0)=[]&lt;br&gt;
&lt;br&gt;
us&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 16 Aug 2007 01:03:52 -0400</pubDate>
      <title>deleting 0s from an array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154622#387911</link>
      <author>James </author>
      <description>&amp;nbsp;&amp;nbsp;Is there any easy way to delete all zero values from an &lt;br&gt;
array (i.e., other than going through each element and &lt;br&gt;
rebuilding)?  &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;This is a 1-D array whose values are used in a &lt;br&gt;
histogram.  Sometimes the bins are too wide in value to get &lt;br&gt;
much information, so I wanted to narrow down by looking at &lt;br&gt;
individual bins with more granularity.  I can find the &lt;br&gt;
maximum value range for the bins, then the next highest, &lt;br&gt;
etc. and generate histograms.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;Or is there a better idea?&lt;br&gt;
&lt;br&gt;
Thanks&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
