4
$\begingroup$

Consider the following table:

a1[x_] = If[x < 1.1, 3, Pi/2]
tabb = ParallelTable[{x, y, z, a1[x]}, {x, 1, 2, 0.1}, {y, 
   0.5, 1, 0.1}, {z, 1, 3, 1}]

Here, a1[x] is some function that mimics some realistic functions; in reality, they may depend on other parameters except for x, and change in a different manner than just with x $>1.1$.

The output looks like {{{row,row,...},{row,row,...}},...},...}:

{{{{1., 0.5, 1, 3}, {1., 0.5, 2, 3}, {1., 0.5, 3, 3}}, {{1., 0.6, 1, 
    3}, {1., 0.6, 2, 3}, {1., 0.6, 3, 3}}, {{1., 0.7, 1, 3}, {1., 0.7,
     2, 3}, {1., 0.7, 3, 3}}, {{1., 0.8, 1, 3}, {1., 0.8, 2, 3}, {1., 
    0.8, 3, 3}}, {{1., 0.9, 1, 3}, {1., 0.9, 2, 3}, {1., 0.9, 3, 
    3}}, {{1., 1., 1, 3}, {1., 1., 2, 3}, {1., 1., 3, 3}}}, {{{1.1, 
    0.5, 1, 3}, {1.1, 0.5, 2, 3}, {1.1, 0.5, 3, 3}}, {{1.1, 0.6, 
    1, \[Pi]/2}, {1.1, 0.6, 2, \[Pi]/2}, {1.1, 0.6, 3, \[Pi]/
    2}}, {{1.1, 0.7, 1, \[Pi]/2}, {1.1, 0.7, 2, \[Pi]/2}, {1.1, 0.7, 
    3, \[Pi]/2}}, {{1.1, 0.8, 1, \[Pi]/2}, {1.1, 0.8, 2, \[Pi]/
    2}, {1.1, 0.8, 3, \[Pi]/2}}, {{1.1, 0.9, 1, \[Pi]/2}, {1.1, 0.9, 
    2, \[Pi]/2}, {1.1, 0.9, 3, \[Pi]/2}}, {{1.1, 1., 1, \[Pi]/
    2}, {1.1, 1., 2, \[Pi]/2}, {1.1, 1., 3, \[Pi]/2}}},...}

It has a block-like structure. I would like to exclude rows for which a1 = $\pi/2$. I may easily do this using Flatten[tabb,{1,2,3}], but the structure of the table would be destroyed (in general, the table may be much more complicated). Is it possible to remove such rows without destroying the structure of the table?

$\endgroup$
1
  • 2
    $\begingroup$ Is DeleteCases[tabb, {___, Pi/2, ___}, {3}] the output you're looking for? $\endgroup$
    – C. E.
    Mar 23 at 20:55
8
$\begingroup$

Since you want to test the last element of each {x, y, z, a1[x]}, I'd modify C.E.'s suggestion in the comments to make sure that cases where Pi/2 appears at any other locations don't get thrown away:

DeleteCases[t, {_, _, _, Pi/2}, {3}] /. {} -> Nothing

This pattern first throws away the structure you don't want, and then the replacement drops the resulting {}-s making it cleaner.

$\endgroup$
3
  • $\begingroup$ Thanks! In DeleteCases[t, {_, _, _, Pi/2}, {3}], could you please tell me the meaning of {3}? $\endgroup$ Mar 23 at 21:42
  • 1
    $\begingroup$ The {3} specifies the Level of each {_,_,_,Pi/2} in the table. The level is 3 because we need 3 indices to extract out such an element. For example, try evaluating tabb[[2,2,1]], and you obtain one such element. Another consistency check to embolden one's faith in the process is: First extract all elements at level {3} by using Level[tabb,{3}], then notice that the length Level[tabb,{3}]//Length is 36 which matches with the product of the 1st 3 numbers from the result of Dimension[tabb]. $\endgroup$ Mar 23 at 21:51
  • 2
    $\begingroup$ If you want to keep the empty lists, then use tabb /. {___, Pi/2} :> Nothing $\endgroup$
    – SHuisman
    Mar 23 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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