Mathematica Mathematica issue: Skip a loop step in Table

AI Thread Summary
The discussion revolves around creating a list of evaluations based on a specific condition in a programming context. The user initially attempts to use a combination of `Table` and `Flatten` to generate a list, but encounters an issue where if the condition is never satisfied, the result is an empty list, which `Flatten` does not alter. The user seeks a solution to either convert this empty result into a zero or modify the `Table` function to skip steps when the condition is not met. A helpful response suggests using an `If` statement to check the length of the resulting list, returning zero if it is empty, or the flattened list otherwise. This solution simplifies the problem and effectively addresses the user's needs.
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 :)
 

Similar threads

Replies
6
Views
4K
Replies
3
Views
3K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
2
Views
6K
Replies
2
Views
3K
Replies
6
Views
3K
Replies
2
Views
2K
Replies
5
Views
2K
Back
Top