multidimensional_arrays - Fast manipulation of...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Fast manipulation of multi-dimensional arrays in Matlab Kevin P. Murphy 11 September 2002 1 Introduction Probabilistic inference in graphical models with discrete random variables requires performing various oper- ations on multi-dimensional arrays (discrete potentials). This is true whether we use an exact algorithm like junction tree [CDLS99, HD96] or an approximate algorithm like loopy belief propagation [AM00, KFL01]. These operations consist of element-wise multiplication/division of two arrays of potentially different sizes, and summation (marginalization) over a subset of the dimensions. This report discusses efficient ways to implement these operations in Matlab, with an emphasis on the implementation used in the Bayes Net Toolbox (BNT). 2 Running example We will introduce an example to illustrate the problems we want to solve. Consider two arrays (tables), Tbig and Tsmall, where Tbig represents a function over the variables X 1 , X 2 , X 3 , X 4 and Tsmall represents a function over X 1 , X 3 . We will say Tbigs domain is (1 , 2 , 3 , 4), Tsmalls domain is (1 , 3), and the difference in their domains is (2 , 4). Let the size of variable X i (i.e., the number of possible values it can have) be denoted by S i . Here is a straighforward implementation of elementwise multiplication: for i1=1:S1 for i2=1:S2 for i3=1:S3 for i4=1:S4 Tbig[i1,i2,i3,i4] = Tbig[i1,i2,i3,i4] * Tsmall[i1,i3]; end end end end Similarly, here is a straightforward implementation of marginalizing the big array onto the small domain: for i1=1:S1 for i3=1:S3 sum = 0; for i2=1:S2 for i4=1:S4 sum = sum + Tbig[i1,i2,i3,i4]; end end Tsmall[i1,i3] = sum; end end 1 Of course, these are not general solutions, because we have hard-coded the fact that Tbig is 4-dimensional, Tsmall is 2-dimensional, and that we are marginalizing over dimensions 2 and 4. The general solution requires mapping vectors of indices (or subscripts) to 1 dimensional indices (offsets into a 1D array), and vice versa. We discuss these auxiliary functions next, before discussing a variety of different solutions based on these functions. 3 Auxiliary functions 3.1 Converting from multi-dimensional indices to 1D indices If a d-dimensional array is stored in memory such that the left-most indices toggle fastest (as in Matlab and Fortran C follows the opposite convention, toggling the right-most indices fastest), then we can compute the 1D index from a vector of indices, subs, as follows: ndx = 1 + (i1-1) + (i2-1)*S1 + (i3-1)*S1*S2 + ... + (id-1)*S1*S2*...*S(d-1) where the vector of indices is subs = ( i 1 , . . . , i d ), and the size of the dimensions are sz = ( S 1 , . . . , S d ). (We only need to add and subtract 1 if we use 1-based indexing, as in Matlab; in C, we would omit this step.) This can be written more concisely as ndx = 1 + sum((subs-1) .* [1 cumprod(sz(1:end-1))]) If we pass the subscripts separately, we can use the built-in Matlab function ndx = sub2ind(sz, i1, i2, ...)ndx = sub2ind(sz, i1, i2, ....
View Full Document

This note was uploaded on 01/14/2011 for the course MATH 571 taught by Professor Staff during the Winter '08 term at University of Michigan.

Page1 / 9

multidimensional_arrays - Fast manipulation of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online