Skip to main content
Engineering LibreTexts

2.1: Matrices

  • Page ID
    14915
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Each variable in Matlab is stored as a matrix, which is an array of numbers arranged in a rectangle of m rows and n columns. One says that such a matrix is an m by n matrix, written as m × n. A vector is any matrix that has either only one row (a “row vector”) or one column (a “column vector”). A scalar, or number, is stored as a matrix that has exactly one row and one column (i.e, a 1 × 1 matrix).

    Let’s look at some examples of how matrices are entered in Matlab. Each matrix is enclosed in the symbols [ and ], each comma (or space) separates entries on the same row and each semicolon indicates a new row.

    Example 2.1.1

    >> A = [1,2,3,4;5,6,7,8] (or A = [1 2 3 4;5 6 7 8])

    gives the 2 × 4 matrix

    A =
    1 2 3 4
    5 6 7 8

    and

    >> B = [1; 6; 0; 9]
    gives the 4 × 1 matrix

    B =
    1
    6
    0
    9
    and

    >> C=[3]

    gives the 1 × 1 matrix (i.e. a scalar)

    C =
    3

    You may have noticed that the semicolon is used in a new way in the last example. It is perhaps unfortunate, but there are symbols that are reused throughout Matlab code (including the semicolon, colon and comma) and the meaning of a particular symbol will depend on context. Semicolons are used inside matrix definitions to indicate a new row while semicolons are used at the end of a line to suppress output. Let’s look at a few more examples.

    Example 2.1.2

    Suppose we wanted a matrix with the values 1 through 19 in a single (row) vector. We could enter this as

    >> M = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]

    although this takes a little while to type (what if we wanted 1 to 1000?!). Instead we use the colon operator. The syntax to create this same matrix of
    1 to 19 is shortened to

    >> M = [1:19].

    In the last example, we can think of the colon as a “range operator.” Note that Matlab also accepts the slightly shorter M = 1:19 as well, which will be useful when we come to loops. In fact, the colon is more flexible (pun intended?) as we see in the next example.

    Example 2.1.3

    The matrix defined as

    >> N = [1:2:19]

    is the same as if we typed

    >> N = [1 3 5 7 9 11 13 15 17 19].

    What is going on in this example? Well, the general form in using two colons this way is

    start value : step size : end value

    In N, the 2 means to simply count “by twos” starting at 1 and ending at 19. There’s a slight limitation in that if we tried P = [1:2:20] we would also get P to equal [1 3 5 7 9 11 13 15 17 19]. Where’s the 20? Well, the syntax of start:step:end actually means to begin at start, add the step one at a time and then stop at the point where adding one more step size would take us past the end value. So, in the matrix P, we have to stop at 19 since adding two more would take us to 21 (past the end value of 20). But, suppose we actually wanted to end exactly at your last value? That’s where we use the command linspace instead.

    Example 2.1.4

    >> P = linspace(1,6,4)

    Here we get

    P =
    1.0000 2.6667 4.3333 6.0000

    You can see that we started at 1 and ended at 6 and have 4 total values. That’s the exact syntax:

    linspace(start,end,number of values)

    There are two special matrices that come up a lot too:

    Example 2.1.5

    >> G = ones(3,4)

    gives the 3 × 4 matrix of all ones:

    G =
    1 1 1 1
    1 1 1 1
    1 1 1 1

    >> H = zeros(2,5)

    gives the 2 × 5 matrix of all zeros:

    H =
    0 0 0 0 0
    0 0 0 0 0


    This page titled 2.1: Matrices is shared under a CC BY-NC 3.0 license and was authored, remixed, and/or curated by Troy Siemers (APEX Calculus) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.