Mathematica issue: Skip a loop step in Table

In summary, the speaker is asking for ideas on how to deal with a situation where a list of evaluations based on a certain function may result in an empty list due to a condition not being satisfied. They suggest using an If statement to check the length of the list and return either 0 or the flattened set, depending on the result.
  • #1
Gueygel
4
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
  • #2
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)
 
  • #3
Thanks Hepth! It wasn't very sophisticated in the end, I really appreciate it :)
 

1. How can I skip a step in a loop using Mathematica's Table function?

The simplest way to skip a step in a loop using Table is to use the If statement. Inside the Table function, you can specify a condition that must be met for a particular step to be executed. If the condition is not met, the step will be skipped.

2. Can I use a Break statement in Table to skip a loop step?

Unfortunately, Mathematica's Table function does not allow the use of Break statements. Instead, you can use the If statement as mentioned in the previous question, or you can use the Skip function to explicitly skip a step.

3. Is there a way to specify which loop step to skip in Table?

Yes, you can specify which step to skip by using the Skip function within the Table function. The first argument of Skip specifies the number of steps to skip, and the second argument specifies the list or range of values to be skipped.

4. Can I use a variable to determine which step to skip in Table?

Yes, you can use a variable in the Skip function to determine which step to skip. The variable should have a numeric value, and you can use it to specify the number of steps to skip.

5. How can I avoid skipping a step in Table if the condition is not met?

If you want to avoid skipping a step in Table, you can use the Null function as the second argument of Skip. This will essentially skip the Skip function, and the loop will continue onto the next step as usual.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top