2
$\begingroup$

I have a nested list of time and position for multiple particles over multiple time steps. I want to cut all the time steps off (for each particle) after which the $z$ coordinate goes above a certain value $zmax$.

So far I've been using the TakeWhile function but that only returns the $z$ values, not the entire list.

Is there a way to get around this?

The structure of the list is

{
  {{t1(0), x1(0), z1(0)}, ..., {t1(n), x1(n), z1(n)}}, 
  {{t2(0), x2(0), z2(0)}, ..., {t2(n), x2(n), z2(n)}}, 
  {{}, ...,                    {tn(n), xn(n), zn(n)}}
}

where the number after the letter is the particle number, and the one in brackets is the time step. If that helps!

For 2 particle example:

Data = {{{0,0,0},{1,10,10},{2,20,20}},{{0,0,0},{1,15,15},{2,30,30}}}. 

Setting the zmax to be 20 for example. Code used:

TakeWhile[Data[[1, All, 3]], # < 40 &]

Which gives: {0,10}. This is only for the first particle, so I presume I'd use some sort of iterative process to do it for all, but I mostly want the output to be {{0,0,0},{1,10,10}} so that every field is displayed.

$\endgroup$
7
  • $\begingroup$ Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – user9660
    Nov 3, 2015 at 16:23
  • 1
    $\begingroup$ Can you give example data, and code showing how you tried to use TakeWhile[]? $\endgroup$ Nov 3, 2015 at 16:29
  • 1
    $\begingroup$ Did you try something like Map[Function[particle, TakeWhile[particle, Last[#] < zmax &]], yournestedlist] (alternatively TakeWhile[#, Last[#] < zmax &] & /@ yournestedlist)? $\endgroup$ Nov 3, 2015 at 16:41
  • $\begingroup$ For 2 particle example: Data = {{{0,0,0},{1,10,10},{2,20,20}},{{0,0,0},{1,15,15},{2,30,30}}}. Setting the zmax to be 20 for example. Code used: TakeWhile[Data[[1, All, 3]], # < 40 &] gives: {0,10}. This is only for the first partile, so I presume I'd use some sort of iterative process to do it for all, but I mostly want the output to be {{0,0,0},{1,10,10}} so that every field is displayed. $\endgroup$
    – jj364
    Nov 3, 2015 at 16:43
  • 1
    $\begingroup$ Edit the question with your example. A better example should show what you expect when a particle in the middle of the row goes out of bounds. (OR perhaps the particles are known to be ordered so they always terminate right to left??) $\endgroup$
    – george2079
    Nov 3, 2015 at 17:18

1 Answer 1

3
$\begingroup$

I used the latter suggestion from @მამუკა ჯიბლაძე's comment:

Did you try something like Map[Function[particle, TakeWhile[particle, Last[#] < zmax &]], yournestedlist] (alternatively TakeWhile[#, Last[#] < zmax &] & /@ yournestedlist)

TakeWhile[#, Last[#] < zmax &] & /@ Data

Although I used the general form for the choice of element (so I could constrain it by x or z) so my actual line was:

TakeWhile[#, #[[3]] < zmax &] & /@ Data

This approach gave me the new list for all the particles and desired time steps. Thank you for your help!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.