Drop elements from list

Mathematica

    Next

  • 1. Finding order of a given differential equation and converting a diff. eqn. to matrix form
    Respected Sir, 1) Is there any function in MATHEMATICA to convert a given linear differential equation to matrix form 2) Is there any function in MATHEMATICA to find the order of a given differential equation.
  • 2. ParametricPlot 3D table
    I need to generate a data table and see ParametricPlot3D (or get table after seeing plot, either way the order does not matter). Just a (u,v,x,y,z) table is adequate. Taking cue from ListPlot for Plot3D I tried: data = Table[N[u+v,u-v,u v/10], {u, 0, 4.5}, {v, 0, 3.5},PlotPoints->{20,20}]; ListParametricPlot3D[data] but echoes inactive. Please help.
  • 3. Coordinate extraction from ListContourPlot
    Hi, I have a list of (x,y) coordinates which I first pass through BinCounts to calculate the number of elements in that list within a specified range. Next, I use ListContourPlot to visualize the distribution with a defined number of contours (easy). What I cannot figure out is how to generate a list of the original (x,y) elements that fall within the boundaries defined by the contours (not so easy?). Apparently, BinCounts does not store the element mappings to specific bins, so I'm out of luck there. Moreover, getting ahold of the contour coordinates themselves is troubling. I've figured out the latter, but remain stuck otherwise. Has anyone else had cause to perfrom such an analysis? Sincerely, Mark
  • 4. Oil spills
    I would like to know if there are any cellular automata models (in Mathmatica of course) for oil spills. --Thanks, V. Stokes
  • 5. Skewed axes on "Show"-ing
    I plotted two 2-D graphs (the range of values taken by the abscissa were the same->[0,0.03].. the functions are well-defined for x<0 also). Both graphs peaked at x=0 decaying quickly as x increased. Peak values: 300 and 400 units for the two. Using "Show" I tried to get a combined graph. What I got was the two graphs.. with the axes of one slightly shifted (horizontally) from the other's causing distortion in the numbers marked on the x and y-axis. I tried reproducing the example provided in the Mathematica book (Section 1.9.3) that involves combining a Bessel and Neumann function (and that too with different ranges for x!!) and it worked! The section said that the axes are supposed to scale appropriately to accomodate both the functions. Question is .. if Mathematica plotted the individual graphs correctly, what went wrong when I simply tried to get the two together? An associated problem (though I do have a guess at the answer).. One of the functions is such that when x is around 0.03, it is already tending to zero.I provided an initial plotting range of [0,0.08] for this function. "Plot" gave me a contracted plot with x going from 0 to 0.03 (approximately). Maybe the command is programmed to remove "tails" (that are considered redundant?) of decaying functions from their plots. .. T/F?

Drop elements from list

Postby SigmundV » Sun, 07 Dec 2008 20:14:27 GMT

Dear group,

Consider a list of length, say, 20:

a = RandomReal[{},20];

How would you then drop/delete elements, say, 2-5 and 7-10 from this
list? The built-in function Drop does not seem to include such a
possibility. I should add that I do not have access to Mathematica 7,
so any solution should work in version 6.

Kind regards,
Sigmund


Re: Drop elements from list

Postby Adriano Pascoletti » Mon, 08 Dec 2008 09:57:06 GMT

Sigmund,
if L denotes a list of pairwise disjoint intervals  a folded Drop
solves the problem:

Fold[Drop[#1, #2] &, a, Reverse@Sort[L]]

Test:

In[3]:= L = {{2, 5}, {7, 10}}; Fold[Drop[#1, #2] & , 100 + Range[20],
Reverse[Sort[L]]]

Out[3]= {101, 106, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120}

Adriano Pascoletti

2008/12/6 SigmundV < XXXX@XXXXX.COM >:


Re: Drop elements from list

Postby Raffy » Mon, 08 Dec 2008 09:57:27 GMT



There are many ways to do this; probably the fastest and most flexible
method would be:

x = Complement[ Range[Length[a]], Range[2,5], Range[7, 10] ];
a[[x]]


Re: Drop elements from list

Postby Jens-Peer Kuska » Mon, 08 Dec 2008 09:58:30 GMT

Hi,

a = Table[b[i], {i, 20}];

and

Drop[Drop[a, {7, 10}], {2, 5}]

and it works ..

Regards
  Jens






Re: Drop elements from list

Postby Bob Hanlon » Mon, 08 Dec 2008 09:59:02 GMT

a = Array[x, {20}];

Drop[Drop[a, {7, 10}], {2, 5}]

{x(1),x(6),x(11),x(12),x(13),x(14),x(15),x(16),x(17),x(18),x(19),x(20)}

Fold[Drop[#1, #2] &, a, {{7, 10}, {2, 5}}]

{x(1),x(6),x(11),x(12),x(13),x(14),x(15),x(16),x(17),x(18),x(19),x(20)}

Or slightly more succinctly with Fold

Fold[Drop[##] &, a, {{7, 10}, {2, 5}}]

{x(1),x(6),x(11),x(12),x(13),x(14),x(15),x(16),x(17),x(18),x(19),x(20)}

% == %% == %%%

True


Bob Hanlon




=============
Dear group,

Consider a list of length, say, 20:

a = RandomReal[{},20];

How would you then drop/delete elements, say, 2-5 and 7-10 from this
list? The built-in function Drop does not seem to include such a
possibility. I should add that I do not have access to Mathematica 7,
so any solution should work in version 6.

Kind regards,
Sigmund



Re: Drop elements from list

Postby dimitris » Mon, 08 Dec 2008 19:42:40 GMT



Hello,

In[33]:=
lst = Table[Random[Integer, {1, 10}], {20}]

Out[33]=
{7,8,9,5,3,2,5,10,7,8,4,7,6,6,9,7,8,7,6,9}

In[34]:=
lst[[Flatten[{1, 6, Range[10, 20]}]]]

Out[34]=
{7,2,8,4,7,6,6,9,7,8,7,6,9}


Re: Drop elements from list

Postby Ray Koopman » Tue, 09 Dec 2008 20:21:32 GMT



I'm surprised no one mentioned Delete, as in

Delete[a, Transpose@Flatten@{Range[2,5],Range[7,10]} ].


Re: Drop elements from list

Postby SigmundV » Wed, 10 Dec 2008 20:56:57 GMT

Thank you for all the solutions. Some of them I should have come up
with myself. My favorite is probably Fold[Drop[##]&,a,{{7,10},{2,5}}].
I don't know whether it is the fastest or not, but it is easy to
program (as in "characters to type"). Ray Koopman's solution with
Delete is also good (although I would like to point out that
Transpose@Flatten@{Range[2,5],Range[7,10]} does not work as it is, but
Partition[...,1] does the trick) and easy to program as well. The idea
presented by Raffy and dimitris, to pick the relevant elements instead
of deleting, is also good. I have discovered that it generally is
faster to pick explicit elements from a list than to use functions as
Drop or Delete. In that respect I like Raffy's solution, but it seems
that too much time is spent in Complement.

Finally, below is a cell with timings of the different solutions. I
would like to hear your comments on this. The final line is included
to see how much time is spent in Complement.

a = RandomReal[{}, 2000000];
Delete[a, Partition[Range[2, 5]~Join~Range[7, 10]~Join~Range[15, 18],
1]]; // Timing
a[[{1, 6}~Join~Range[11, 14]~Join~Range[19, Length[a]]]]; // Timing
Fold[Drop[##] &, a, {{15, 18}, {7, 10}, {2, 5}}]; // Timing
a[[Complement[Range[Length[a]], Range[2, 5], Range[7, 10], Range[15,
18]]]]; // Timing
Complement[Range[Length[a]], Range[2, 5], Range[7, 10], Range[15,
18]]; // Timing

/Sigmund






Re: Drop elements from list

Postby Ray Koopman » Thu, 11 Dec 2008 18:45:46 GMT



I omitted a pair of curly brackets. I meant to write
  Transpose@{Flatten@{Range[2,5],Range[7,10]}}.


a = RandomReal[{}, 2*^7];
b1 = Delete[a, Partition[Join[Range[2,5],Range[7,10],
                              Range[15,18]],1]]; // Timing
b2 = a[[Join[{1,6},Range[11,14],Range[19,Length@a]]]]; // Timing
b3 = Fold[Drop[##]&, a, {{15,18},{7,10},{2,5}}]; // Timing
b4 = a[[Complement[Range@Length@a, Range[2,5],Range[7,10],
                                   Range[15,18]]]]; // Timing
b5 = a[[komplement[Length@a, Join[Range[2,5],Range[7,10],
                                  Range[15,18]]]]]; // Timing
SameQ[b1,b2,b3,b4,b5]
c1 = Complement[Range@Length@a, Range[2,5],Range[7,10],
                                Range[15,18]]; // Timing
c2 = komplement[Length@a, Join[Range[2,5],Range[7,10],
                                Range[15,18]]]; // Timing
SameQ[c1,c2]

{0.55,Null}
{1.88,Null}
{1.81,Null}
{8.91,Null}
{3.65,Null}
True
{7.81,Null}
{2.,Null}
True

If your lists will usually be so long, with so few elements to
be deleted, it looks like deleting will be faster than taking.




Re: Drop elements from list

Postby Ray Koopman » Thu, 11 Dec 2008 18:45:57 GMT

Correcting an omission from my previous post:




Here's an adaptation of Carl Woll's "missing" from his July 15, 2005,
post in the thread "Complement replacement":

komplement[n_, list_] := Block[{r = Range@n}, r[[list]] = 0;
           SparseArray[r] /. SparseArray[_,_,_,x_]:>x[[3]]]


Re: Drop elements from list

Postby Raffy » Thu, 11 Dec 2008 18:46:18 GMT




It's not really a fair comparison, as only the Complement can handle
any combination of ranges, even malformed ranges.
Delete/Partition solution comes close, but still will fail if you give
it malformed range.
The other solutions suggested require that the ranges are non-
overlapping, reverse sorted, and well-formed.

To have the benefit of being able to give the Fold/Drop solution with
any combination of well-formed ranges, you could do:

ranges = { Range[2,5], Range[7,10], Range[15, 18] };

Fold[Drop, a, Reverse[Split[Union @@ ranges, #2 - #1 == 1 &][[All, {1,
-1}]]]]

ie. this would let you do something like: { Range[2, 10], Range[3,
11], Range[15,18] };



Return to Mathematica

 

Who is online

Users browsing this forum: No registered users and 86 guest