Mathematica issue: Skip a loop step in Table

  • Context: Mathematica 
  • Thread starter Thread starter Gueygel
  • Start date Start date
  • Tags Tags
    Loop Mathematica Table
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 5K views
Gueygel
Messages
4
Reaction score
0
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!
 
Physics news on Phys.org
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)
 
Thanks Hepth! It wasn't very sophisticated in the end, I really appreciate it :)