Binary singleton expansion function for gpuArray
C = bsxfun(FUN, A, B)
This method of a gpuArray object is similar in behavior to the MATLAB® function bsxfun, except that the actual evaluation of the function happens on the GPU, not on the CPU.
C = bsxfun(FUN, A, B) applies the element-by-element binary operation specified by the function handle FUN to arrays A and B, with singleton expansion enabled. If A or B is a gpuArray, bsxfun moves all other required data to the GPU and performs its calculation on the GPU. The output array C is a gpuArray, whose data you can retrieve with gather.
The corresponding dimensions of A and B must be equal to each other, or equal to one. Whenever a dimension of A or B is singleton (equal to 1), bsxfun virtually replicates the array along that dimension to match the other array. In the case where a dimension of A or B is singleton and the corresponding dimension in the other array is zero, bsxfun virtually diminishes the singleton dimension to 0.
The size of the output array C is such that each dimension is the larger of the two input arrays in that dimension for nonzero size, or zero otherwise. Notice in the following code how dimensions of size 1 are scaled up or down to match the size of the corresponding dimension in the other argument:
R1 = rand(2,5,4,'gpuArray'); R2 = rand(2,1,4,3,'gpuArray'); R = bsxfun(@plus,R1,R2); size(R)
2 5 4 3
R1 = rand(2,2,0,4,'gpuArray'); R2 = rand(2,1,1,4,'gpuArray'); R = bsxfun(@plus,R1,R2); size(R)
2 2 0 4
Subtract the column means from each element of a matrix:
A = rand(8,'gpuArray'); M = bsxfun(@minus,A,mean(A));