How to assign different names to array elements RRS feed

  • Question

  • I have a 2-D array, how can I assign a name to each element?

    Range rowR = new Range(2); Range colR = new Range(5); var myV = Variable.Array(Variable.Array<int>(colR),rowR); for (int i=0; i<rowR.SizeAsInt; i++)


    myV[i][0] = Variable.Discrete(SomeProbDist).Named(("u "+i+",0")); for (int j=1; j<colR.SizeAsInt; j++) myV[i][j] = Variable.Discrete(SomeProbDist).Named(("v "+i+","+j));


    The above code doesn't seem to work.


    Wednesday, June 10, 2015 12:37 AM

All replies

  • You can't assign a different name to each element of the array, because under the hood the Infer.NET compiler sees the array as a whole object. Did you mean to assign a different name to the prior of each element? The way to name the array is as follows:

                Range rowR = new Range(2);
                Range colR = new Range(5);
                var myV = Variable.Array(Variable.Array<int>(colR), rowR).Named("MyRandomJaggedArray");

    You can then define the elements of the array. In order to stop confusing C# code with Infer.NET modelling code, please look carefully at this User Guide section and its subsections. You'll then find out the foreach loops look more like this:

                using (var blockRow = Variable.ForEach(rowR))
                    using (var blockCol = Variable.ForEach(colR))
                        var isFirst = blockCol.Index == 0;
                        using (Variable.If(isFirst))
                            myV[rowR][colR] = Variable.Discrete(someProbDist).Named(("u " + blockRow.Index + ",0"));
                        using (Variable.IfNot(isFirst))
                            myV[rowR][colR] = Variable.Discrete(someProbDist).Named(("v " + blockRow.Index + "," + blockCol.Index));


    Wednesday, June 10, 2015 9:42 AM