Addition of 2 matrices with different dimensions

조회 수: 47 (최근 30일)
Christos Tsallis
Christos Tsallis 2021년 3월 12일
댓글: Christos Tsallis 2021년 3월 12일
Hello,
If we want to add 2 matrices in maths, their dimensions must be the same.
I was wondering how MATLAB can add x=[1;5;9] which has dimensions of 3x1 with y=[9 2 8] which has dimensions 1x3 and the result is x+y=[10 3 9;14 7 13;18 11 17] which has dimensions 3x3.
Thanks for your time, appreciate your help.

채택된 답변

Allen
Allen 2021년 3월 12일
In this instance your are describing, MATLAB is assuming element-wise addition between the rows and columns. Where
Where, x =
[x1
x2
x3];
and y = [y1 y2 y3];
The result of x+y then becomes:
[x1+y1 x1+y2 x1+y3
x2+y1 x2+y2 x2+y3
x3+y1 x3+y2 x3+y3]
  댓글 수: 2
Star Strider
Star Strider 2021년 3월 12일
This is due to ‘Automatic Implicit Expansion’ introduced in R2016b.
See ‘Implicit Expansion’ under ‘Mathematics’ ina the Release Notes.
Christos Tsallis
Christos Tsallis 2021년 3월 12일
Thanks a lot, but is this mathematically correct?

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

추가 답변 (1개)

Steven Lord
Steven Lord 2021년 3월 12일
If we want to add 2 matrices in maths, their dimensions must be the same.
I believe most texts accept a slight generalization of that, to allow adding scalars to matrices that are not 1-by-1. Scalar expansion has been part of MATLAB for longer than I've been at MathWorks (nearly 20 years.) It's probably in Cleve's original Fortran MATLAB.
A = magic(3)
A = 3×3
8 1 6 3 5 7 4 9 2
B = A + 1 % If we applied the idea you stated this should be A + ones(3)
B = 3×3
9 2 7 4 6 8 5 10 3
Implicit expansion is a generalization of that behavior. It avoids having to repmat the vectors to a common size we can compute, thus saving memory. After all, if A in my example above took up 1 GB of space (which would mean B would also take 1 GB of space) do you really want to have to allocate a temporary 1 GB matrix all of whose elements are 1?
Thanks a lot, but is this mathematically correct?
I won't tell the Mathematics Police if you don't.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by