Select Page

Arrays

We can represent the location of a point in three-dimensional space by three ” Cartesian coordinates .r, y, and z. As shown in Figure 2.1-1, these three coordinates specify the vector p. (In mathematical text we often use boldface type to indicate vectors.) The set of unit vectors i,j, k, whose lengths are 1 and whose directions coincide with the x, y, and z axes, respectively, can be used to express the vector mathematically as follows: p =xi +yj +zk. The unit vectors nable us to associate the vector components x, y, z with the proper  oordinate axes; therefore, when we write p = 5i + 7j + 2k, we know that the x, y, and z coordinates of the vector are 5, 7, and 2, respectively. We can also write the components in a specific order, separate them with a space, and identify the group with brackets, as follows: [5 7 2]. As long as we agree that the vector components will be written in the order x, y, z, we can use this notation instead of the unit-vector notation. In fact, MATLAB uses this style for vector notation. MATLAB allows us to separate. .We can represent the location of a point in three-dimensional space by three ” Cartesian coordinates .r, y, and z. As shown in Figure 2.1-1, these three coordinates specify the vector p. (In mathematical text we often use boldface type to indicate vectors.) The set of unit vectors i,j, k, whose lengths are 1 and whose directions coincide with the x, y, and z axes, respectively, can be used to express the vector mathematically as follows: p =xi +yj +zk. The unit vectors enable us to associate the vector components x, y, z with the proper coordinate axes; therefore, when we write p = 5i + 7j + 2k, we know that the x, y, and z coordinates of the vector are 5, 7, and 2, respectively. We can also write the components in a specific order, separate them with a space, and identify the group with brackets, as follows: [5 7 2]. As long as we agree that the vector components will be written in the order x, y, z, we can use this notation instead of the unit-vector notation. In fact, MATLAB uses this style for vector notation. MATLAB allows us to separate.

Figure 2:1-1 Specification of a position vector Using Cartesian coordinates

the components with commas for improved readability if we desire so that the equivalent way of writing the preceding vector is [5, 7, 2]. This expression is a row vector, which is a horizontal arrangement of the elements.We can also express the vector as a column vector, which has a vertical arrangement, as follows:

A vector can have only one column, or only one row. Thus, a vector is a special case of an array. In general, arrays can have more than one column and more than’ one row.

Creating Vectors in MATLAB

Although a position vector cannot have more than three components, the concept of a vector can be generalized to any number of components. In MATLAB a vector is simply a list of scalars, whose order of appearance in the list might be significant, as it is when specifying xyz coordinates. As another example, suppose we measure the temperature of an object once every hour. We can represent the measurements as a vector, and the 1Oth element in the list is the temperature measured at the 10th hour.

To create a row vector in MATLAB, you simply type the elements inside a pair of square brackets, separating the elements with a space or a contma. Brackets are required for arrays in some cases, but not all. To improve readability, we will always use them. The choice between a space or comma is a matter of personal.

preference, although the chance of an error is less if you use a comma. (You can also use a comma followed by a space for maximum readability.) To create a column vector, you can separate the elements by semicolon .

Alternatively, you can create a row vector and then use the transpose notation (,), which converts a row vector into a column vector, or vice versa. For example:

The third way to create a column vector is to type a left bracket ( [) and the fir t element, press Enter, type the second element, press Enter, and so on until you type the last element followed by a right bracket () ) and Enter. On the’ screen this sequence looks like.

Note that MATLAB displays row vectors horizontally and column vectors vertically.

You can create vectors by “appending” one vector to another. For example, to create the row vector u whose first three columns contain the values of r = [ 2 , 4 , 20) . and whose fourth, fifth, and sixth columns contain the values of w = [ 9 , – 6 , 3 ) ,  you type u = [ r ,w) . The result is the vector u = [2,4,20,9,-6,3).
The colon operator ( 🙂 easily generates a large vector of regularly spaced elements. Typing »x = [m:q:n]

creates a vector x of values with a spacing q. The first value is m. The last value is n if m – n is an integer multiple of q. If not, the last value is less than n. For example, typing x = [ 0 : 2 : 8) creates the vector x = [ 0 , 2 I 4 , 6, 8) • whereas typing x = [ 0 : 2 : 7) creates the vector x = [ 0 , 2 , 4 , 6 ] . To create a row vector z consisting of the values from 5 to 8 in steps of 0.1, you type z [ 5 : 0 . 1:8] . If the increment q is omitted, it is presumed to be 1. Thus y = [- 3 : 2] produces the vector y = [- 3 , – 2 , -1, 0 , 1, 2 ) .

The increment qcan be negative. In this case m should be greater than n. For example, u = [ 10 : – 2 : 4] produces the vector [10, 8 , 6 , 4] . The 1inspace command also creates a linearly spaced row vector, but instead you specify the number of values rather than the increment. The syntax is linspace (xl, x2, n), where xl and x2 are the lower and upper limits and n is the number of points. For example, 1inspace ( 5, 8, 31) is equivalent to [5 : 0 . 1 : 8] . If n is omitted, the spacing is I. The logspace command c’f.e~esl;an array of logarithmically spaced el- , j’ ., ements. Its syntax is logspace (a ,.,b, n ) ; where n is the number of points between 10″ and 10″ For example, x = lgspace (-1,1,4) produces the vector x = [0.1000, 0.4642, 2~?;544, 10.000].Ifnisomitted,the number of points defaults to 50.

Two-Dimensional Arrays

An array is a collection of scalars arranged in a logical structure. An array may have a single row; if so, it is called a row vector. A column vector has a single column. An array can have multiple rows, multiple columns, or both. Such a two-dimensional array is called a matrix. In mathe’w~tical text we often use boldface type to indicate vectors and matrices, for e~’!idiple,A. If possible, vectors • -I’ are usually denoted by lowercase letters and matrices by  ppercase letters. An example of a matrix having three rows and two columns is

A matrix should not be confused with a determinant-which also has rows and columns but can be reduced to a single number. Two parallel lines usually , denote a determinant; square brackets usually denote matrices. _ We refer to the size of an array’ by the number of rows and the number of •  columns. For example, an array with 3 rows and 2 columns is said to be a 3 x 2 ——- array. The number of rows is always stated first! A row vector has a size  f-I x n where 1:1 is the number of columns. A column vector has a size of n x I where n’ is the number of rows.

We sometimes represent a matrix A as [ai)] to indicate its elements aij). The subscripts i and j-called indices-indicate the row and column location of the element aij. The row number must always come first! For example, the element a32 is in row 3. column 2. Two matrices A and B are equal if they have the same size and if all their corresponding elements are equal; that is, a;’j =bi) for every value of i and j .

Creating Matrices

The most direct way to create a matrix is to type the matrix row by ow, separating the elements in a given row with spaces or commas and separating the rows with semicolons. For example, typing.

»A = [2,4,10;16,3,7]; 3,7 ] ;

creates the following matrix:

Remember, spaces’or commas separate elements in different columns, whereas semicolons separate elements in different rows.
If the matrix has many elements, you can press Enter and continue typing
on the next line. MATLAB knows you are finished entering the  atrix when you type the closing bracket (] ).
You can also create a matrix from row or column vectors. A row vector r can be appended to a matrix A if r and A have the same number of columns. The command B = [A r] appends the row vector r to the matrix A. This increases the number of columns in A. Use a semicolon to increase the number of rows. The command B = [Ai r] appends the row vector r to the matrix A. Note the difference between the results given by [a b) and [a i b) in the following session:,

You need not use symbols to create a new array. For example, typing D = [ [ 1 , 3 I 5] i [7 I 9 I 11] ] ; produces the same result as typing D = [ a i b) .

Matrices and the Transpose Operation

The transpose operation interchanges the rows and columns.In mathematics text . we denote this operation by the superscript T. For an m x n matrix A with m rows and n columns, AT (read “A transpose”) is an n x m matrix. For example, if

then

If AT = A, the matrix A is symmetric. Only a square matrix can be symmetric, but not every square matrix is symmetric.
If the array contains complex elements, the transpose operator ( , )  roduces the complex conjugate transpose; that is, the resulting elements are the complex conjugates of the original array’s transposed elements. Alternatively, you can use the dot transpose operator (. ,) to transpose the array without producing complex conjugate elements, for example, A   If all the elements are real, the operators’ and . ‘ give the same result.

Array indices are the row and column numbers of an element in an array and are used to keep track of the array’s elements. For example, the notation v (5) refers to the fifth element in the vector v, and A (2, 3) refers to the element in row 2, column 3 in the matrix A. The. row number is always listed first! This notation enables you to correct entries in an array without retyping the entire array. For example, to change the element in row 1, column 3 of a matrix D to 6, you can type D (1, 3) = 6.
The colon operator selects individual elements, rows, columns, or  subarrays”
of arrays. Here are some examples:  v ( : ) represents all the row or column elements of the vector v.  v (2 : S) represents the second through fifth elements; that is v (2 ) , v (3 ) , v(4),v(5).
A( : , 3) denotes all the elements in the third column of the matrix  .
A ( : , 2 : 5) denotes all the elements in the second through fifth  columns
of A ( 2 : 3 , 1 : 3) denotes all the elements in the secend and third rows that are also in the first through third columns.
You can se array indices to extract a smaller array from another  array. For example, If you create the array B

by typing
»B = [2,4,10,13;16,3,7,18;8,4,9,25;3,12,15,17];
and then type
>>C = B(2:3,1:3);

you can produce the following array

The empty or null array contains no elements and is expressed as []. Row\ and columns can be deleted by setting the selected row or column equal to the null array. This step causes the original matrix to collapse to a smaller one. For example, A (3, 🙂 = [] deletes the third row in A. while A C: ,2: 4) = [] deletes the second through fourth columns in A. Finally, A ( [1 4],:) = [] deletes the first and fourth rows of A. Suppose we type A = [ 6 , 9 , 4 i 1 , 5 , 7] to define the following matrix:

Typing A ( 1 , 5) = 3 changes the matrix to

Because A did not have five columns, its size is automatically expanded to accept the new element in column 5. MATLAB adds zeros to fill out the remaining elements.
MATLAB does not accept negative or zero indices, but you can use  egative
increments with the colon operator. For example, typing B = A ( : , 5 : -1 : 1 )
reverses the order of the columns in A and produces

Suppose that C = [-4,12,3,5,8]. Then typing B (2, 🙂 = C replaces row
2 of B with c. Thus B becomes

Suppose that D = [ 3 , 8 , 5 ; 2 , – 6 , 9]. Then typing E = D ( [2 , 2 , 2] , : ) repeats row 2 of D three times to obtain.

Using clear to Avoid Errors

You can use the clear command to protect yourself from accidentally reusing an array that has the wrong dimension. Even if you set new values for an array, some previous values might still remain. For example, suppose you had previously used the 2 x 2 array A = [ 2, 5; 6, 9], and you then create the 5 x I arrays x = [ 1 : 5] , and y = [ 2 : 6] , . Suppose you now redefine A so that its columns will.

be x and y. If you then type A ( : I 1) = x to create the first column, MATLAB displays an error message telling you that the number of rows in A and x must be the same. MATLAB thinks A should be a 2 x 2 matrix because A was previously defined to have only two rows and its values remain in memory. The clear command wipes A and all other variables from memory and avoids this error. To clear A only, type clear A before typing A( : 11) = x.

Some Useful Array Functions

MATLAB has many functions for working with arrays (see Table 2.1-1). Here is a summary of some of the more commonly used functions. The max (A) function returns the algebraically greatest element in A if A is a vector having all real elements. It returns a row vector containing the greatest elements in each column if A is a matrix containing all real elements. If any of the elements are complex, max (A) returns the element that has the largest

Table 2.1-1 Array functions

magnitude. The syntax Ix , k] = max(A) is similar to max(A), but it stores the maximum values in the row vector x and their indices in the row vector k. The functions min (Al and [x, k] = min (Al are the same as ax …. and [x, k] = max(Al except that they return minimum values. The function size (Al returns a row vector [m n] containing the sizes of the m x n array A. The length (Al function computes either the number 0 elements of A if A is a vector or the largest value of In or Il if A is an m x n matrix. For example, if

then max(A) returns the vector [6, 2] ; min (Al returns the vector [- _  -51; size (A) returns [.3,2]; and length (Al returns 3. If A has one or more complex elements, max(A) returns the element that has the largest magnitude. For example, if

Then max (Al returns the vector [-10, – 5] and min (A) returns the vector [3 +4i, 0]. (The magnitude of 3 +4i is 5.)
The sum (A) function sums the elements in each column of the array  and returns a row vector containing the sums. The sort (Al function sorts each column of the array A in ascending order and returns an array the same size as A.The find (x) command computes an array containing the indices of the no ll zero elements of the vectorx. The syntax [u,v,w] = find(A) computes the arrays u and v, containing the row and column indices of the nonzero elements of the matrix A, and the array.w, containing the values of the nonzero elements.the array w may be omitted. For example, if

then the session
»A = [6, 0, 3; 0, 4, 0; 2, 7, 0];
»[u, v, w] = find(Al
returns the vectors

The vectors u and v give the (row, column) indices of the nonzero values, which are listed in w. For example, the second entries in u and v give the indices (3, 0, which specifies the element in row 3, column 1 of A, whose value is 2. These functions are summarized in Table 2.1-1.

Magnitude, Length, and Absolute Value of a Vector

The terms magnitude, length, and absolute value are often loosely used in everyday language, but you must keep their precise meaning in mind when using MATLAB. The MATLAB length command gives the number of elements in the vector. The magnitude of a vector x having elements XI, X2,,Xn is a scalar, given by xt +x + … +x and is the same as the vector’s geometric length. The absolute value of a vector x is a vector whose elements are the absolute values of the elements of x. For example, if x = [ 2 I – 4 I 5 ] , its length is 3; its magnitude is .) 22 +(-4) 2 +52 = 6.7082; and its absolute value is ‘[ 2 I 4 I 5].

The Array Editor

We saw some commands such as who, whos, and exist, that are useful for managing the workspace. The MATLAB Workspace Browser provides a graphical interface for managing the workspace. You can use the Workspace Browser to view, save, and clear workspace variables. It also includes the Array Editor, a graphical interface for working with arrays. To open the Workspace Browser, do one of the following:

• From the Desktop menu in the MATLAB Desktop, select  workspace;
• Click the Workspace tab below the Current Directory window; or
• type workspace at the Command window prompt.

Figure 2.1-2 The Workspace Browser

The browser appears as shown in Figure 2.1-2. Workspace operations you can perform with the Workspace Browser or with functions are:

1. Viewing and editing the workspace variables.
2. Clearing workspace variables. 3. Plotting workspace variables.