# TakeWhile for nested list

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.

• 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!
– user9660
Nov 3, 2015 at 16:23
• Can you give example data, and code showing how you tried to use TakeWhile[]? Nov 3, 2015 at 16:29
• Did you try something like Map[Function[particle, TakeWhile[particle, Last[#] < zmax &]], yournestedlist] (alternatively TakeWhile[#, Last[#] < zmax &] & /@ yournestedlist)? Nov 3, 2015 at 16:41
• 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. Nov 3, 2015 at 16:43
• 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??) Nov 3, 2015 at 17:18

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!