12.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}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)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.
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
%% 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.
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.
.
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.
.
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.