Re: Parts of a list
- To: mathgroup at smc.vnet.net
- Subject: [mg15471] Re: Parts of a list
- From: dreissBLOOP at bloop.earthlink.net (David Reiss)
- Date: Mon, 18 Jan 1999 04:22:11 -0500
- Organization: EarthLink Network, Inc.
- References: <77jkve$304@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In article <77jkve$304 at smc.vnet.net>, "Hossein Kazemi"
<kazemi at som.umass.edu> wrote:
> I have a list (matrix) that looks like this
>
> x={{1,0},{2.5, .5},{3,0},{0,2}}
>
> Now I want to eliminate those vectors that have zero for the second
> case. That is, I want to get
>
> {{2.5, .5},{0,2}}.
>
> Is there a quick way of doing this?
> I tried to apply Select to this but could not get it to work.
>
> Thanks for your help.
>
> Hossein Kazemi
> UMass
> kazemi at som.umass.edu
I will change your data example just a bit. It includes both repeated
cases and non-exact zeros (floats) as well as exact zeros. In[1]:=
x={{1,0},{1,0},{9,0},{1,0.},{2.5, .5},{3,0},{0,2}}
Out[1]=
{{1,0},{1,0},{9,0},{1,0.},{2.5,0.5},{3,0},{0,2}}
The following is ths shortest possibility that I can come up with that
will work:
In[2]:=
x/.{_,0|0.}:>Sequence[]
Out[2]=
{{2.5,0.5},{0,2}}
If you only want to remove the dases where there are exact zeros then
use
In[3]:=
x/.{_,0}:>Sequence[]
Out[3]=
{{1,0.},{2.5,0.5},{0,2}}
A somewhat more complicated possibility is
In[4]:=
x//.{w:{_,_}...,{_,0|0.},p:{_,_}...}:>{w,p}
Out[4]=
{{2.5,0.5},{0,2}}
Note that the ReplaceRepeated ("//.") is needed so that all of the cases
are removed
In[5]:=
?ReplaceRepeated
"expr //. rules repeatedly performs replacements until expr no longer
changes."
This only would go part of the way
In[6]:=
x/.{w:{_,_}...,{_,0|0.},p:{_,_}...}:>{w,p}
Out[6]=
{{1,0},{9,0},{1,0.},{2.5,0.5},{3,0},{0,2}}
--
David Reiss
dreissBLOOP at bloop.earthlink.net
To send personal email, remove the words "bloop" and "BLOOP" from the
email address