Skip to main content
Engineering LibreTexts

11.6: 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}\) Oil well resistivity

    When drilling for oil, engineers measure the resistivity of the material that they are drilling thru.

    A section with a resistivity resistivity > 2 Ohms/m indicates oil or gas.

    1. (2 pts) Enter the following array into a MATLAB or Octave script file.

    The 1st column is the depth of drilling.

    The 2nd column is the resistivity.

    D_R = [

    1.000 0.300

    1.010 0.296

    1.020 0.298

    1.030 0.286

    1.040 0.251

    1.050 0.280

    1.060 0.500

    1.070 0.700

    1.080 1.000

    1.090 2.000

    1.100 3.000

    1.110 4.692

    1.120 4.872

    1.130 4.743

    1.140 5.070

    1.150 4.627

    1.160 4.881

    1.170 5.396

    1.180 4.712

    1.190 4.000

    1.200 1.900

    1.210 1.500

    1.220 1.000

    1.230 0.962

    1.240 0.954

    1.250 0.953

    1.260 1.002];

    2. (2 pts) Create a variable named depth = the 1st column’s data using array indexing.

    3. (2 pts) Create a variable named resistivity = the 2nd column’s data using array indexing.

    4. (2 pts) Use the find() function to find the indices of the values of resistivity > 2 Ohms/m.

    Call this vector of indices oil_index.

    5. (2 pts) Use the these indices with the depth vector to create and display a vector of the corresponding oil depths. Call this vector oil_depths

    References:

    https://www.slb.com/resource-library/oilfield-review/defining-series/defining-log-interpretation

    https://upload.wikimedia.org/wikipedia/commons/thumb/e/e9/Oil_Rig_NT8.svg/394px-Oil_Rig_NT8.svg.png

    Answer

    This is for the student to solve.

    .

    Exercise \(\PageIndex{2}\) 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.6: 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.

    • Was this article helpful?