# Can some one provide a link to examples on the combining use of Variable.switch and jagged array? • ### Question

• I know the Recommender system example shows the use of jagged array. And I have a basic idea of what variable.switch  means according to the API documentation. Can anyone provide some examples on the use of variable.switch on the jagged array?

I wrote some codes that can be compiled by C# compiler, however, there are model compilation errors when execution. So I wish to see how others use variable.switch syntax.

Thanks!

Tuesday, June 9, 2015 1:28 AM

### All replies

• What do you mean by a switch on a jagged array? Nested switching? The Wet Grass/Sprinkler/Rain example does something similar (you'll have to see the code in our ). Also, the documentation on variable branching can be found here. If this still doesn't answer your question, let me know, and I'll write a sample switch on a jagged array for you.

-Y-

Tuesday, June 9, 2015 8:32 AM
• I have a 2-D variable array.

Variables within each row have dependency, while variables across different rows are independent.

Each variable has categorical distribution. We also have a group of categorical distributions. The number of distributions is equal to the number of categories. Within each row, the distribution of v[i][j] is distribution[v[i][j-1]]. For each row i, v[i] is not dependent on any other variables.

To apply such a dependence, switch and jagged array are involved.

Tuesday, June 9, 2015 11:48 PM
• Since variables across rows are independent, you can do a Variable.ForEach over the first dimension of the jagged array, and within the loop treat the nested one-dimensional arrays as you would typically do. This should remove the complexity coming from jagged arrays, and you should be able to write the Variable.Switch as you would normally do.

```            Range i = new Range(x);
Range j = new Range(y);
var randomJaggedArray = Variable.Array(Variable.Array<double>(j), i);
using (Variable.ForEach(i))
{
var randomArray = randomJaggedArray[i];
// Do stuff with the one-dimensional randomArray
}```

-Y-

Wednesday, June 10, 2015 10:26 AM