Binary singleton expansion function for gpuArray


C = bsxfun(FUN,A,B)


bsxfun with gpuArray input is similar in behavior to the MATLAB® function bsxfun, except that the actual evaluation of the function, FUN, 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, which you can copy to the MATLAB workspace with gather.

For more detailed information, see Run Element-wise MATLAB Code on GPU. For the subset of the MATLAB language that is currently supported by bsxfun on the GPU, see Supported MATLAB Code.

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);
  2     5     4     3
R1 = rand(2,2,0,4,'gpuArray');
R2 = rand(2,1,1,4,'gpuArray');
R = bsxfun(@plus,R1,R2);
  2     2     0     4


Subtract the mean of each column from all elements in that column:

A = rand(8,'gpuArray');
M = bsxfun(@minus,A,mean(A));
Was this topic helpful?