Search All of the Math Forum:

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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

 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

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