Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica issue: Skip a loop step in Table

  1. Apr 19, 2010 #1
    Hi everyone,

    I have to do a list made of evaluations of a certain function, but if some condition is not satisfied, then I don't want to have the corresponding evaluation in my list. So I tried:

    Flatten[Table[If[condition,f,{}],{i,imin,imax}]]

    "condition" is true or false depending on the value of "i". If it is not satisfied, then an empty {} is written and Flatten takes it away. But sometimes "condition" never gives True for any value of "i" between imin and imax. In that case, my list turns to be {{}}, and Flatten does nothing.

    I would like either {{}} to be a zero (but just in this case, as there are other zeros due to other reasons), or something able to tell Table to skip a step if "condition" is not satisfied.

    Does anyone have any idea to deal with this?


    Thanks in advance!
     
  2. jcsd
  3. Apr 19, 2010 #2

    Hepth

    User Avatar
    Gold Member

    Just do a :

    If[Length[BLAH]==0,0,BLAH]

    so if the length of that set is 0 it returns 0, else it returns the flattened set (where BLAH is the set)
     
  4. Apr 19, 2010 #3
    Thanks Hepth! It wasn't very sophisticated in the end, I really appreciate it :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Mathematica issue: Skip a loop step in Table
  1. Mathematica issue (Replies: 4)

  2. Mathematica looping (Replies: 3)

  3. Mathematica: Tables (Replies: 2)

Loading...