4.3: Creating and Indexing a 2D Array
- Page ID
- 136621
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \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{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
The most common way to create a matrix in MATLAB is to type the values inside square brackets. Values in the same row are separated by spaces or commas. Rows are separated by semicolons.
mat1 = [4, 3, 1; 3, 8, 6] mat2 = [2 10 5; 6 20 17; 13 2 0]
The first matrix has 2 rows and 3 columns. The second matrix has 3 rows and 3 columns. It is often helpful to line up the entries visually when you type a matrix, especially when the matrix has several rows:
mat2 = [ 2 10 5;
6 20 17;
13 2 0 ];
MATLAB does not require the extra spaces, but spacing can make your code easier to read.
Every row of a matrix must have the same number of elements. If one row has 3 values and another row has 4 values, MATLAB cannot create a rectangular 2D array.
Using the Colon Operator to Create Rows
The colon operator can also be used inside a matrix. This is convenient when the rows follow a pattern.
Solution
The first row is created from 2 through 5, and the second row is created from 3 through 6. Both rows have four elements, so MATLAB can combine them into a matrix.
mat3 = 2 3 4 5 3 4 5 6
Indexing in 2D Arrays
Indexing means referring to a specific part of an array. For a 2D array, we use two indices: one for the row and one for the column.
Solution
This asks MATLAB for the value in row 1, column 3. In this case, the answer is 1.
Remember: In MATLAB, matrix indexing is row first, column second: matrix(row, column).
For example, in matrix mat2, the value 17 is in row 2, column 3.
|
Column 1 |
Column 2 |
Column 3 |
|
|
Row 1 |
2 |
10 |
5 |
|
Row 2 |
6 |
20 |
17 |
|
Row 3 |
13 |
2 |
0 |


