Why is 4-by-2 matrix compatible with 4-by-1 vector?

10 次查看(过去 30 天)
Why is 4-by-2 compatible with 4-by-1 as per https://www.mathworks.com/help/matlab/matlab_prog/compatible-array-sizes-for-basic-operations.html ? I thought it has to be mxn matrix and nx1 vector.
x = [1 2;3 4;5 6; 7 8].*[1;2;3;4]
This results to 4x2 matrix. What does the ".*" do?
If I transpose 4x2 matrix then multiply it answer is 2x1 matrix and not 4x2.
  1 个评论
Stephen23
Stephen23 2020-7-29
编辑:Stephen23 2020-7-29
"I thought it has to be mxn matrix and nx1 vector."
Correct: for matrix multiplication those would be suitable sizes.
"What does the ".*" do? "
Element-wise array multiplication. You are confusing two very different groups of numeric operations:
To use MATLAB you have to learn the difference.

请先登录,再进行评论。

回答(3 个)

Sriram Tadavarty
Sriram Tadavarty 2020-7-29
Hi Ann,
As placed in the documentation link, yes, it is compatible.
The .* operation does element wise multiplication.
For the example, you provided x, here is how the code works
x = [1 2;3 4;5 6; 7 8].*[1;2;3;4]
% Firstly the first column elements are multiplied with the elements of second matrix, with each element.
% Implies
% 1*1 is the first element in the first column
% 3*2 is the second element in the first column
% 5*3 is the thrid element in the first column
% 7*4 is the fourth element in the first column
% Now, the same applies for second column
% 2*1 is the first element in the second column
% 4*2 is the second element in the second column
% 6*3 is the third element in the second column
% 8*4 is the fourth element in the second column
It is an element-wise multiplication. If there is a normal matrix multiplication (*), then it must satify the matrix multiplication rule as stated by you.
If you transpose the matrix, it doesn't directly work with element wise operation but can perform matrix multiplication. Since, (2 x 4) matrix multiplied by (4 x 1), the column size of first matrix is same as row matrix. thus, matrix multiplication is possible.
But, for element wise multiplication, the number of rows must be the same, if the first element is a matrix with number of rows and columns greater than 1. If the first element is a column vector and second element is a row vector, it must be similar to matrix multiplication.
In simple terms, the element-wise operation takes place element by element and to perform it, each column element must be multiplied with the same column element of second matrix.
Hope this helps.
Regards,
Sriram

John D'Errico
John D'Errico 2020-7-29
编辑:John D'Errico 2020-7-29
You are using .* to do the multiplication. NOT * which is the linear algebra operator.
MATLAB extends the vector implicitly into a 4x2 array, and THEN does an element-wise multiply.

Walter Roberson
Walter Roberson 2020-7-29
This is because of "implicit expansion".
It used to be the case (in R2016a and earlier) that when you did binary operations such as + or .* between two arrays, that the sizes had to match in every dimension. As of R2016b, the rules changed, and operations became similar to as-if bsxfun() had been used (but implemented differently internally.)
For implicit expansion (and bsxfun), the rule is no longer that the sizes must match exactly: the rule instead is that the sizes must match except that a dimension in one of them may be 1 when the other one is not 1, and that when that happens, the one that is 1 will be internally copied as many times as needed to match the size of the other operand.
So with a 4 x 2 matrix A being operated on together with a 4 x 1 matrix B, then the operation would be carried out "as if" you had done A .* repmat(B, [1 size(A,2)]), so for your calculation the result would be the same as if you had used
x = [1 2;3 4;5 6; 7 8].*[[1;2;3;4], [1;2;3;4]]

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by