matrix - MATLAB sub2ind using vectors -
suppose have matrix a
a = magic(5) 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
now select block of matrix using
a(1:2, 1:2) 17 24 23 5
now need linear index given (1:2, 1;2) (1 2 6 7). using sub2ind:
sub2ind(size(a),[1:2], [1:2])
but command returns (1 7) how can solve this?
suppose want select a(1:2,2:3)
:
% row , column indexes rind = 1:2; cind = 2:3; pos = bsxfun(@plus,rind', size(a,2)*(cind-1)); pos = 6 11 7 12
you might want reshape column vector pos(:)
, or in 1 line call reshape()
.
Comments
Post a Comment