Skip to main content
Engineering LibreTexts

11.5: Matrix Tables and Nested Loops

  • Page ID
    85038
  • \( \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}}\)

    By Carey A. Smith

    A table with 2 or more related rows or columns can be created as a matrix, as shown in these examples.

    Example \(\PageIndex{1}\) Row matrix table of squares and cubes

    x1 = 13:16
    % x1 = 13 14 15 16
    x2 = x1.^2 % square each element of x1
    % x2 = 169 196 225 256
    x3 = x1.^3 % cube each element of x1
    % x3 = 2197 2744 3375 4096
    % These can be put into a single variable that is a horizontal table relating these values:
    x_table = [x1
    x2
    x3]
    % x_table =
    % 13 14 15 16
    % 169 196 225 256
    % 2197 2744 3375 4096

    Solution
    Example \(\PageIndex{2}\) Column matrix table of squares and cubes

    %% Often,a vertical table is preferred, especially if there are a lot of values.
    y1 = 11:20;
    y2 = y1.^2; % square each element of y1
    y3 = y1.^3; % cube each element of y1
    % A vertical table is created by transposing each horizontal vector into a vertical vector:
    y_table = [y1' y2' y3']
    % y_table =
    % 11 121 1331
    % 12 144 1728
    % 13 169 2197
    % 14 196 2744
    % 15 225 3375
    % 16 256 4096
    % 17 289 4913
    % 18 324 5832
    % 19 361 6859
    % 20 400 8000

    Solution

    Add example text here.

    Example \(\PageIndex{3}\) km to miles matrix table

    %% Example of km to miles
    % In this case, each vector is created as a vertical vector, so the transpose is not needed when creating the table:
    km = (1:10)' % The ' is the transpose operator.
    % km =
    % 1
    % 2
    % 3
    % 4
    % 5
    % 6
    % 7
    % 8
    % 9
    % 10
    km2miles = 0.6214 % km per mile
    miles = km2miles*km
    % miles =
    % 0.6214
    % 1.2428
    % 1.8642
    % 2.4856
    % 3.1070
    % 3.7284
    % 4.3498
    % 4.9712
    % 5.5926
    % 6.2140
    km_2_miles_table = [km miles]
    % km_2_miles_table =
    % 1.0000 0.6214
    % 2.0000 1.2428
    % 3.0000 1.8642
    % 4.0000 2.4856
    % 5.0000 3.1070
    % 6.0000 3.7284
    % 7.0000 4.3498
    % 8.0000 4.9712
    % 9.0000 5.5926
    % 10.0000 6.2140

    Solution

    Add example text here.

    Exercise \(\PageIndex{1}\) Even-Odd Nested Loops

    Watch this video:

    Then code the nested for-loops shown in the video for the even-odd algorithm, with these changes:

    1. Delete this line A = input('Please enter an array > '). Instead, code your own 3x4 matrix.

    2. Since i is the imaginary number = sqrt(-1), use ii and jj for the loop indices.

    3. Test your code.

    Answer

    Add texts here. Do not delete this text first.


    This page titled 11.5: Matrix Tables and Nested Loops is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.