Obtain matrix of indices in octave / matlab -


given multidimensional matrix in octave / matlab,

what's easiest way matrix of same size elements replaced index along k'th dimension

ie matrix

a =  ans(:,:,1) =     0.095287   0.191905    0.226278   0.749100  ans(:,:,2) =     0.076826   0.131639    0.862747   0.699016 

i want function f such f(a,1) =

ans(:,:,1) =     1   1    2   2  ans(:,:,2) =     1   1    2   2 

f(a,2) =

ans(:,:,1) =     1   2    1   2  ans(:,:,2) =     1   2    1   2 

and

f(a, 3) =

ans(:,:,1) =     1   1    1   1  ans(:,:,2) =     2   2    2   2 

also, given sparse matrix b

what's easiest way sparse matrix of same size nonzero elements replaced index along k'th dimension? (so same problem above, nonzero elements)

ideally i'm looking way well-vectorized octave (meaning doesn't explicitly loop on anything)

clarification: sparse matrix one, i'm looking solution not involve creating full size(b) matrix @ point

ndgrid() want, although not in format looking for. if know dims of input beforehand, can use following line create n-dimentional mesh grid:

% matrix ndims(a) == 3 [x, y, z] = ndgrid (1:size(a,1), 1:size(a,2), 1:size(a,3)); % x f(a, 1) % y f(a, 2) % z f(a, 3) 

you may able write custom wrapper around ndgrid() convert function format looking for.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -