Question about indexing multi-dimensional matrices

Przemek Klosowski przemek at jazz.ncnr.nist.gov
Mon Sep 8 13:40:25 CDT 2008


   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;

Therefore, you can just wrap it up in a single line:

	   A(num2cell([1 2 3]){:})=2



More information about the Help-octave mailing list