How to use Switch with variable number of terms?

In summary, you can use pattern-matching syntax to make your Switch statement more concise and efficient.
  • #1
aheight
321
109
Hi,

I'd like to use Switch with an integer partition of a number. For example, if n=4, then I define the partition as:
monodromyTypes = IntegerPartitions[n];
this gives:
monodromyTypes={{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}}. I'd then like to set up a Switch depending on what the monodromy is so I set up the Switch codes as:

switchCodes =
Flatten[Table[{monodromyTypes[[i]], i}, {i, 1,
Length[monodromyTypes]}], 1]

this gives me:

{{4}, 1, {3, 1}, 2, {2, 2}, 3, {2, 1, 1}, 4, {1, 1, 1, 1}, 5}

However just passing that array to Switch won't work. I have to sequence it or something else but I don't know how to format it so it would work with Switch.

I was wondering if someone could help me with this? For example, if monodromy={2,2}, then I want to format Switch as:

Switch[monodromy,{4}, 1, {3, 1}, 2, {2, 2}, 3, {2, 1, 1}, 4, {1, 1, 1, 1},5]

and this would return 3.

Thanks.[/I]
 
Physics news on Phys.org
  • #2
You can use the pattern-matching syntax to make this work. Instead of writing out all the cases explicitly, you can use a pattern such as `monodromy_:<some list>` to match a given list. This way, you don't need to specify each case explicitly.For example:Switch[monodromy, monodromy_:{4}, 1, monodromy_:{3, 1}, 2, monodromy_:{2, 2}, 3, monodromy_:{2, 1, 1}, 4, monodromy_:{1, 1, 1, 1}, 5]This will return the appropriate value depending on the list given.
 

Related to How to use Switch with variable number of terms?

1. How does the Switch function work with a variable number of terms?

The Switch function in programming allows for conditional execution of code based on the value of a variable. When using a variable number of terms, the Switch function can be used to execute different code blocks depending on the value of the variable.

2. Can the Switch function handle an unlimited number of terms?

No, the Switch function has a limit on the number of terms it can handle. This limit varies depending on the programming language and implementation, but it is usually a few hundred terms.

3. How do you specify the number of terms in a Switch statement?

The number of terms in a Switch statement is determined by the number of cases specified within the statement. Each case represents a different value of the variable being evaluated, and the Switch function will execute the code block associated with the case that matches the value of the variable.

4. What happens if none of the cases in a Switch statement match the value of the variable?

If none of the cases in a Switch statement match the value of the variable, the default case will be executed. This is a case that is specified as default and does not require a specific value to be matched. If there is no default case, the Switch function will not execute any code and will move on to the next line of code.

5. Can the Switch function be used with non-numeric values?

Yes, the Switch function can be used with non-numeric values as long as the cases are specified to match the type of the variable. For example, if the variable is a string, the cases should also be strings. This allows for more flexibility in using the Switch function with a variable number of terms.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
342
  • Engineering and Comp Sci Homework Help
Replies
13
Views
476
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
304
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
747
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top