Question about indexing multi-dimensional matrices

Moreno Marzolla moreno.marzolla at pd.infn.it
Fri Sep 5 14:14:02 CDT 2008


Hello everybody,

I have a small problem with multi-dimensional matrices which I solved 
somehow, but was wondering whether a better solution exists.

I'm trying to implement some Queueing Network analysis algorithms using 
GNU octave (see http://www.dsi.unive.it/~marzolla/software/qnetworks/ ). 
One of such algorithms requires handling multi-dimensional matrices for 
which both the number of dimensions and the size of each dimension are 
passed as parameters. So, the caller invokes the function by passing an 
array N such that length(N) is the number of dimensions, and N(i) is the 
size (number of elements) along dimension i.

Unfortunately, indexing a matrices with apriori unknown dimension is 
tricky. If I define an array idx = [1 3 2], then given a 3x3x4 matrix A, 
the Octave documentation says that A( idx ) is not A(1,3,2) (I tried, 
and the documentation is of course correct).

Representing the matrix A as a 3x3x4 elements vector, and using the 
sub2ind function to get a linear index has the same problem, as sub2ind 
expects a variable number of separate parameters for each dimension 
index. So

sub2ind( [3 3 4], idx )

does not produced the expected result, while

sub2ind( [3 3 4], 1,3,2 )

does.

To solve this problem I wrote a function very similar to sub2ind which 
accepts an array as the second argument.

Is there a way to use the standard sub2ind() function (or some 
appropriate indexing expression) to access an element whose coordinates 
are given as a vector? If not, would it be the case to modify (overload) 
the standard Octave sub2ind() function such that it also accepts two 
vector parameters, as in the first example above?

Thanks for any suggestion,

Moreno.

-- 
Moreno Marzolla
EMail: moreno.marzolla at pd.infn.it
WWW  : http://www.dsi.unive.it/~marzolla



More information about the Help-octave mailing list