help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question about indexing multi-dimensional matrices


From: Jaroslav Hajek
Subject: Re: Question about indexing multi-dimensional matrices
Date: Fri, 5 Sep 2008 22:39:33 +0200

On Fri, Sep 5, 2008 at 9:14 PM, Moreno Marzolla
<address@hidden> wrote:
> 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,
>

use a cs-list argument to achieve what you need:

adims = [3,3,4]
aind = [1,2,3]

aind = num2cell (aind);
ind = sub2ind (adims, aind{:})

regards

-- 
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz


reply via email to

[Prev in Thread] Current Thread [Next in Thread]