Mathematica issue: Skip a loop step in Table

  • Context: Mathematica 
  • Thread starter Thread starter Gueygel
  • Start date Start date
  • Tags Tags
    Loop Mathematica Table
Click For Summary
SUMMARY

The discussion revolves around handling conditional evaluations in Mathematica's Table function. The user seeks a method to skip evaluations based on a condition, specifically when the condition is not satisfied for any value of 'i' within the specified range. The solution proposed involves using the If function to check the length of the resulting list and return 0 if it is empty. This approach effectively addresses the issue of receiving an empty list when no conditions are met.

PREREQUISITES
  • Familiarity with Mathematica programming language
  • Understanding of conditional statements in programming
  • Knowledge of list manipulation functions in Mathematica
  • Basic experience with the Table function in Mathematica
NEXT STEPS
  • Explore advanced list manipulation techniques in Mathematica
  • Learn about the use of the If function in Mathematica
  • Investigate the behavior of the Table function with different conditions
  • Study error handling and default values in Mathematica
USEFUL FOR

This discussion is beneficial for Mathematica users, particularly those involved in data analysis or algorithm development, who need to implement conditional logic in their evaluations.

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 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K