Matlab introduces bsxfun (binary singleton expansion) since 2007, which made matrix-vector operation easier when the vector is going to be applied to each row.
In fact, bsxfun works with all binary operations, if it needs to be done element-wisely.
Usually, when mxArray A and mxArray B are going to take operation op(A, B), but the dimension does not match, it might need repmat to reallocate memory.
>> a = rand(40);
>> a - mean(a); % matrix - vector, error
Error using -
Matrix dimensions must agree.>> a = rand(40);
>> bsxfun(@minus, a, mean(a)); % runs fineRefer to official site.
funcan also be a handle to any binary element-wise function not listed above. A binary element-wise function of the formC = fun(A,B)accepts arraysAandBof arbitrary, but equal size and returns output of the same size. Each element in the output arrayCis the result of an operation on the corresponding elements ofAandBonly.
The corresponding dimensions of
AandBmust be equal to each other or equal to one. Whenever a dimension ofAorBis singleton (equal to one),bsxfunvirtually replicates the array along that dimension to match the other array. In the case where a dimension ofAorBis singleton, and the corresponding dimension in the other array is zero,bsxfunvirtually diminishes the singleton dimension to zero.
The size of the output array
Cis equal to:max(size(A),size(B)).*(size(A)>0 & size(B)>0).
This technique vectorizes the binary operation, and skip the overhead of allocation of memory as repmat does.