Question about indexing multi-dimensional matrices

Jaroslav Hajek highegg at gmail.com
Sat Sep 6 07:33:32 CDT 2008


On Sat, Sep 6, 2008 at 2:02 PM, Moreno Marzolla
<moreno.marzolla at pd.infn.it> wrote:
> Jaroslav Hajek wrote:
> [...]
>>
>> 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{:})
>
> Thank you very much, it works!
> Actually, it is even better. Apparently I can use num2cell to index
> multidimensional matrices directly (instead of using a vector + sub2ind),
> like this:
>
> N = [3 3 4];
> A = zeros(N);
> idx = [1 3 2];
> iidx = num2cell(idx);
> A( iidx{:} ) = 1;
>
> Regards,

Yes, of course. This is called "Fortran" or "linear" indexing and is,
by default, available for any array.

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



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


More information about the Help-octave mailing list