Mathematically it is not possible to add a column vector with a row vector . However, when i tried to code this logic in Matlab it creates a square matrices of both the vectors and adds them .

Vivek on 26 Aug 2025
Latest activity Reply by xingxingcui on 2 Sep 2025

a = [ 1 2 3 ]
a =
1 2 3
b = [ 3; 5; 8]
b =
3
5
8
x = a + b
x =
4 5 6
6 7 8
9 10 11
xingxingcui
xingxingcui on 2 Sep 2025
Similarly, it's not only MATLAB that has this kind of implicit expansion — other languages have similar features too, for example Python; this is called 'broadcasting.
The advantages of this design are concise expression, better performance, and easy extensibility.
Walter Roberson
Walter Roberson on 27 Aug 2025
Mathematically the result of adding a scalar and a matrix is not defined. Mathematically you have to do a matrix multiplication of the scalar by ones(M,1) and then another matrix multiplication by an (1,N) matrix to copy the scalar to be (M,N) before you can do the addition. None the less, MATLAB choses to define the result of adding a scalar to a matrix, converting undefined behaviour to defined behaviour. And it sure is convenient that MATLAB chose to do so.
Mathematically the result of adding a row vector and a column vector is not defined. None the less, MATLAB chooses to define the result of adding a row vector to a column vector... and it is useful sometimes.
Consider for example the definition of uint8. MATLAB defines the result of adding two sufficiently large uint8 to "saturate", returning uint8(255). MATLAB could have defined the result of adding two sufficiently large uint8 to result in promotion of the result to uint16. MATLAB could have defined the result of adding any two uint8 to be uint16 (in case of overflow), and could have defined the result of adding any two uint16 to be uint32, and could have defined the result of adding any two uint32 to be uint64... but after that, it would be stuck. Unless, that is, MATLAB defined the result of adding any two values to be the "algebraic" result, using some kind of internal indefinite precision arithmetic. Which would not be efficient... and would not be practical either once you get into square roots or trigonometry.
MATLAB is a programming language. Programming languages are not limited to exactly replicate mathematical behaviour.
Walter Roberson
Walter Roberson on 28 Aug 2025
By the way: adding a scalar to a 3D array cannot be defined mathematically using matrix multiplication. Matrix multiplication is only defined for 2D arrays. So there is no mathematical way of defining 1 + ones(2,2,2) ... but it is clearly very convenient that such an operation be defined.
Stephen23
Stephen23 on 28 Aug 2025
Binary floating point numbers also do not follow "mathematics".
Associative law may fail
a = +1e16;
b = -1e16;
c = 1;
x = (a + b) + c
x = 1
y = a + (b + c)
y = 0
Adding to a number doesn’t change it
x = 1e17;
d = 1;
y = x + d;
y-x
ans = 0
etc. etc. Numeric computations are similar to high-school mathematics, but certainly not the same.
Steve Eddins
Steve Eddins on 26 Aug 2025 (Edited on 27 Aug 2025)
@Vivek, in addition to the Nick Higham article that @Mike Croucher mentioned, you might be interested in more details about this behavior and its history. Some resources:
I encourage you to read through the comments in reply to my two guest blog posts. There is a lot of good discussion there among MATLAB users with a variety of opinions.
Here are some other MATLAB Central blog posts that demonstrate the use of implicit expansion:
Finally, I'll note that MATLAB has a very long history of taking mathematical operators and expanding (pun intended) their meaning in order to facilitate easy, expressive computations on matrices. For example:
  • x = B/A solves for x
  • A + s, for scalar s, adds s to every element of the matrix A.
Mike Croucher
Mike Croucher on 26 Aug 2025
Nice summary Steve! I remember using bsxfun to accelerate MATLAB code for users back before I joined MatHWorks but implicit expansion has rendered it obsolete.
Steve Eddins
Steve Eddins on 26 Aug 2025
bsxfun was introduced by the MATLAB Math Team around 2006 because Cleve's original implicit expansion proposal (circa 2001), although approved, had been indefinitely delayed because of technology and prioritization reasons. Discussion about this started up again in 2012-2014. By then, the many uses of bsxfun, by both MathWorks developers and MATLAB users, provided many concrete examples for us to consider.
Mike Croucher
Mike Croucher on 26 Aug 2025
What you are seeing here is implict expansion. The late Nick Higham discusses it here Implicit Expansion: A Powerful New Feature of MATLAB R2016b – Nick Higham