Vectorize functions on regions of array Octave/ Matlab -


i vectorize following octave code:

a = 1:100; b = [20 40 60]; c = zeros(3,11);  = b,     c(i,:) = a( (b(i) - 10) : b(i) ); end 

which extracts sub-arrays starting @ specific indexes longer array.

i tried:

c = a(b - 10,b); 

but returns first sub-array.

thanks

how about

>> c = a( bsxfun( @plus, -10:0, b' ) );  c =      10    11    12    13    14    15    16    17    18    19    20     30    31    32    33    34    35    36    37    38    39    40     50    51    52    53    54    55    56    57    58    59    60 

if don't have bsxfun in octave, can repmat

c = a( repmat( -10:0, [3 1] ) + repmat( b', [1 11] ) );  

ps,
best not use i variable in matlab.


Comments