필터 지우기
필터 지우기

How does bsxfun actually work ?

조회 수: 21 (최근 30일)
Shounak Shastri
Shounak Shastri 2018년 2월 14일
편집: Shounak Shastri 2018년 2월 15일
I tried to multiply two matrices using bsxfun. Here is a sample code.
a = [0,1];
b = [1,2];
q = bsxfun(@times, a, b);
Once I run this code, I get the output
[0 2]
So I tried the following combinations
q1 = bsxfun(@times, a, b');
q2 = bsxfun(@times, a', b);
q3 = bsxfun(@times, a', b');
and all gave me a valid output and none of these generate a multiplication error.
The help says the function uses "singleton expansion" which, I suppose, means it inserts a singleton dimension in either of the two matrices to make the multiplication possible.
Can someone explain what exactly "singleton expansion" means and how is it used in this function?

채택된 답변

Stephen23
Stephen23 2018년 2월 14일
편집: Stephen23 2018년 2월 14일
"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:
size(out) = [4,9,5]
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.
  댓글 수: 1
Shounak Shastri
Shounak Shastri 2018년 2월 15일
Thank you. This clears a lot of things.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Rik
Rik 2018년 2월 14일
편집: Rik 2018년 2월 14일
From the release notes (about implicit expansion, introduced in R2016b, replacing most use cases for bsxfun):
Implicit expansion is a generalization of scalar expansion. With scalar expansion,
a scalar expands to be the same size as another array to facilitate element-wise operations.
With implicit expansion, the element-wise operators and functions listed here can implicitly expand
their inputs to be the same size, as long as the arrays have compatible sizes. Two arrays have
compatible sizes if, for every dimension, the dimension sizes of the inputs are either
the same or one of them is 1.
The point is illustrated by your example. In the case of q1 and q2 the input sizes don't match, so Matlab replicates the vectors to match the needed size.
%starting with this:
a.*b'
%substitute actual vectors:
[0,1].*[1;2]
%expand to match sizes:
[0,1;0,1].*[1,1;2,2]
%result:
[0,1;0,2]
%(equivalent to this:)
%repmat(a, size(b',1), 1).*repmat(b', 1, size(a,2))
  댓글 수: 1
Shounak Shastri
Shounak Shastri 2018년 2월 15일
편집: Shounak Shastri 2018년 2월 15일
Thank you for your answer. The example helps a lot.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by