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
8 Comments
Time DescendingSimilarly, 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.
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.
@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:
- From the MATLAB doc: "Compatible Array Sizes for Basic Operations"
- My 24-Oct-2016 guest post on Loren Shure's Art of MATLAB blog: "MATLAB arithmetic expands in R2016b"
- My 10-Nov-2016 follow-up post on Loren's blog: "More thoughts about implicit expansion"
- Loren's 19-Dec-2019 post: "Importance of Implicit Expansion For Performance"
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:
- Chessboards, Implicit Expansion, REPELEM, and Unicode Chess Queens
- sinpi, cospi, implicit expansion and The 2022 MATLAB Mini-Hack
- Treble clefs, Unicode, SVG, strings, Bézier curves, kron, implicit expansion, and polyshape
- Feret Diameter: Introduction
- How to Compute Perceptual Color Difference
- Singing the Praises of Strings
- Repeated Indexing in MATLAB
- Use less meshgrid and repmat
- Once and for All
- Gourds to Graphics: The MATLAB Pumpkin
- Symmetry, tessellations, golden mean, 17, and patterns
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.
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
Sign in to participate