matlab - How can I divide columns of a matrix by elements of a vector, element-wise? -
given matrix , vector
a = [ 1 2; 2 4 ]; v = [ 2 4 ]; how can divide each column of matrix respective element of vector? following matrix should be
[ 1/2 2/4; 2/2 4/4 ] basically want apply column-wise operation, operands each column stored in separate vector.
you should use combination of rdivide , bsxfun:
a = [ 1 2; 2 4 ]; v = [ 2 4 ]; b = bsxfun(@rdivide, a, v); rdivide takes care of per element division, while bsxfun makes sure dimensions add up. can achieve same result like
b = ./ repmat(v, size(a,1), 1) however, using repmat results in increased memory usage, why bsxfun solution preferable.
Comments
Post a Comment