Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Re: [mg4180] How can this MapAt application be done more efficiently
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
Mary Vlastnik Armon

Posts: 1
Registered: 12/11/04
Re: [mg4180] How can this MapAt application be done more efficiently
Posted: Jun 16, 1996 2:16 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply



On Tue, 11 Jun 1996, Joel Cannon wrote:

>
> I have written a function to Drop any zero's from a table of numbers.
> Here is an example:
>
> In[161]:= dropzero[ {{5, 0}, {4, 1}, {3, 2, 0}, {3, 1, 1}} ]
>
> Out[161]= {{5}, {4, 1}, {3, 2}, {3, 1, 1}}
>



Here's something that appears to work, and it doesn't depend on the
position of 0 in the sublists:

In[25]:=
dropzero1[a_] := DeleteCases[a,(x_/;x==0),2]

In[26]:=
dropzero1[ {{5, 0}, {4, 1}, {3, 2, 0}, {3, 1, 1}} ]
Out[26]=
{{5}, {4, 1}, {3, 2}, {3, 1, 1}}

It deletes everything equal to 0 at level 2 of the list you give it, so
you'd have to change the level specification for more deeply nested lists.
If you found it necessary to do that, you could make it an option in the
function definition and set the default level as 2:

In[39]:=
dropzerolevel[a_,levels_:2]:= DeleteCases[a,(x_/;x==0),levels]

In[40]:=
list={{5, 0}, {4, 1}, {3, 2, {0,Pi}}, {3, 1, 1}};

Here, you only drop the zeros at level 2:

In[41]:=
dropzerolevel[list]

Out[41]=
{{5}, {4, 1}, {3, 2, {0, Pi}}, {3, 1, 1}}

Here, you drop the zeros at levels 2 and 3:

In[42]:=
dropzerolevel[list,{2,3} ]

Out[42]=
{{5}, {4, 1}, {3, 2, {Pi}}, {3, 1, 1}}

Also, I think the difficulty you were having with MapAt and Drop was that
you needed Drop[#,-1]& (the "&" is what you appeared to be missing.)


This may be way more than you wanted to know, but I hope it helps--

Mary Vlastnik Armon
Dept. of Mathematics
Knox College
Galesburg, IL 61401







Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.