Subtracting a Vector from a Scalar-Multiplied Matrix

Let's say that we have the following two matrices and we want to calculate . We know that it is not defined, but MATLAB gives me the following result.Could anyone explain me why that happens?
C=[1 2; 2 1]
C = 2x2
1 2 2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
E=[1;2]
E = 2x1
1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
3*C-E
ans = 2x2
2 5 4 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

5 Comments

@Voss Thank you, I have seen this before! But I still can't figure out how to show this in Matlab to my students and make them realize that this is not defined
Why do you write it is not defined ? It is defined (in MATLAB) and is the same as if E were set to
E = [1 1;2 2]
@Torsten Yes this is my problema tha it is defined in Matlab but not in Maths.
This is what I want to show to my students
It's just a convention to get rid of the bsxfun - there is nothing to explain (or even justify).

Sign in to comment.

 Accepted Answer

8 Comments

Thank you, I have seen this before! But I still can't figure out how to show this in Matlab to my students and make them realize that this is not defined. Do I have to use bsxfun?
"not defined" where?
If you're saying that it's not defined mathematically, then by the technical definition of matrix multiplication (which requires that the number of columns in the first matrix being multiplied must equal the number of rows in the second matrix) neither is multiplying a 1-by-1 matrix 2 by a 2-by-2 matrix [1 2;3 4].
A = 2;
B = [1 2; 3 4];
isdefined = size(A, 2) == size(B, 1)
isdefined = logical
0
C = A*B
C = 2x2
2 4 6 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
But scalar-matrix multiplication (via scalar expansion) is a useful extension of matrix multiplication, isn't it?
This is another useful extension of a number of different operations.
Oh!!! Ok thank you very much for your explanation
Multiplication of a matrix by a scalar is mathematically based by the rule f(a*x) = a*f(x) of a linear operator f. It's not an extension of matrix multiplication.
@Torsten @Steven Lord My actuall question was why Matlab does this subtraction, because E is a column vector.
Because can't directly subtract a column vector to a matrix because the operations of matrix addition require the two operands to have the same dimensions. In matrix subtraction, corresponding elements are subtracted to each other, so both matrices need to have the same number of rows and columns.
C=[1 2; 2 1]
C = 2x2
1 2 2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
E=[1;2]
E = 2x1
1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C-E
ans = 2x2
0 1 0 -1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
My actuall question was why Matlab does this subtraction, because E is a column vector.
In release R2016b we made the decision to start allowing this type of operation in MATLAB as long as the inputs are compatibly sized even if they are not identically sized.
Because can't directly subtract a column vector to a matrix because the operations of matrix addition require the two operands to have the same dimensions.
Not anymore in MATLAB. We require compatible sizes not identical sizes.
Oh ok!! Now it is all clear! Thank you!
Even prior to R2016B, as far back as to the very first release as far as I know, Matlab implemented scalar expansion. Code like this has always worked
eye(2) - 1
ans = 2x2
0 -1 -1 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
even though subtracting a scalar from a matrix is not mathematically rigorous.

Sign in to comment.

More Answers (0)

Products

Release

R2024a

Community Treasure Hunt

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

Start Hunting!