"Can someone explain what exactly "singleton expansion" means and how is it used in this function?"
bsxfun (and many operators that support this since R2016b) expands any singleton dimension to match the dimension of the other array. For example, lets say you have two arrays with the following sizes:
size(A) = [4,1,5]
size(B) = [4,9,1]
Note how the second dimension of A and the third of B are both equal to 1 (i.e. they are singletons). Then using bsxfun (or the new operators) to perform any binary operation will expand those singleton dimensions to match the size of that dimension of the other array, giving an output with size:
Thus the singleton dimensions have been expanded. Note that it is called singleton expansion because it only applies to dimensions with size 1, and is NOT (currently) generalized to other non-matching sizes. For example, this would be an error:
size(A) = [3,1,5]
size(B) = [4,9,1]
because the first dimensions are NOT singleton and have different sizes.
Best Answer