I am confused about array multiplication and divistion

6 views (last 30 days)
Hello,
I am really confused by the use of (.) when multiplying and dividing arrays, i've looked at the documentation and I am still confused.
For example: 1) Why does 3/[1 2 3] produce an error, while 3*[1 2 3] does not.
And
2) Why does [1 2 3]./[1 2 3] = 1 1 1, while [1 2 3]/[1 2 3] = 1
Also
3) [1 2 3].*[1;2;3] = error while [1 2 3]*[1;2;3] = 14
Thanks!!

Answers (2)

Guillaume
Guillaume on 11 Dec 2015
dotted operations (so, .* and ./) are memberwise operations, that is the 1st element of each operand are multiplied (or divided) together, the 2nd elements operate together, and so on. Therefore both operand must be the same size and shape. Matlab makes an exception when one is scalar, the scalar is replicated as necessary to match the size and shape of the other operand. So, for
A .* B
A ./ B
to work, you must satisfy
all(size(A) == size(B)) %both operand same size and shape
%or
isequal(size(A), [1 1]) || isequal(size(B), [1 1]) %or at least one operand is scalar
Matrix multiplication (so just *, no dot) is a well defined mathematical operation. It is completely different from the dotted multiplication and is only defined when the first matrix has the same number of columns as the second matrix. See there for a very good introduction to matrix products.
Matrix division (just /, no dot) is not a valid mathematical operation. Matlab reuses the operator as a way to solve linear equation, so
A / B
finds the x so that
xB = A
So it works completely differently than ./.
See also the documentation for each operator:

Walter Roberson
Walter Roberson on 11 Dec 2015
scalar * vector and vector * scalar are treated specially in MATLAB for convenience. It is a convenience that is common in computer languages. On the other hand, 3 * [1 2 3] is a 1 x 1 item being matrix-multiplied by a 1 x 3 item, so even if you were to stick with strict definitions, because the inner dimensions agree, the 1 x 3 result would be perfectly well defined. MATLAB could have insisted that scalar * column_vector be an error, but added the flexibility to define it as being the same as scalar .* column_vector . Allowing it to have a meaning does not interfere with any defined use of algebraic matrix multiplication .
scalar / vector is not treated specially in MATLAB. That is just a design choice. That is partly because scalar / column_vector is meaningful in a way that is different than scalar ./ column_vector. Treating scalar / vector as being equivalent to scalar ./ vector would interfere with a defined use of mrdivide
The / operator is formally known as mrdivide; see http://www.mathworks.com/help/matlab/ref/mrdivide.html
"x = B/A solves the system of linear equations x*A = B for x. The matrices A and B must contain the same number of columns."
The ./ operator is formally known as rdivide; see http://www.mathworks.com/help/matlab/ref/rdivide.html
"x = A./B divides each element of A by the corresponding element of B"
The * operator is formally known as mtimes; see http://www.mathworks.com/help/matlab/ref/mtimes.html
"C = A*B is the matrix product of A and B"
"This definition says that C(i,j) is the inner product of the ith row of A with the jth column of B."
The .* operator is formally known as times; see http://www.mathworks.com/help/matlab/ref/times.html
"C = A.*B multiplies arrays A and B element by element"

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!