# Assignment Operator Matlab Matrix

, , , and are common indexing expressions for a matrix that contain a colon. When you use a colon as a subscript in an indexing expression, such as , it acts as shorthand to include *all* subscripts in a particular array dimension. It is also common to create a vector with a colon for the purposes of indexing, such as . Some indexing expressions combine both uses of the colon, as in .

Common indexing expressions that contain a colon are:

is the th column of matrix .

is the th row of matrix .

is the th page of three-dimensional array .

reshapes all elements of into a single column vector. This has no effect if is already a column vector.

reshapes all elements of into a two-dimensional matrix. This has no effect if is already a matrix or vector.

uses the vector to index into and is therefore equivalent to the vector .

includes all subscripts in the first dimension but uses the vector to index in the second dimension. This returns a matrix with columns .

## Multidimensional Arrays

### Overview

An array having more than two dimensions is called a multidimensional array in the MATLAB^{®} application. Multidimensional arrays in MATLAB are an extension of the normal two-dimensional matrix. Matrices have two dimensions: the row dimension and the column dimension.

You can access a two-dimensional matrix element with two subscripts: the first representing the row index, and the second representing the column index.

Multidimensional arrays use additional subscripts for indexing. A three-dimensional array, for example, uses three subscripts:

The first references array dimension 1, the row.

The second references dimension 2, the column.

The third references dimension 3. This illustration uses the concept of a

*page*to represent dimensions 3 and higher.

To access the element in the second row, third column of page 2, for example, you use the subscripts .

As you add dimensions to an array, you also add subscripts. A four-dimensional array, for example, has four subscripts. The first two reference a row-column pair; the second two access the third and fourth dimensions of data.

Most of the operations that you can perform on matrices (i.e., two-dimensional arrays) can also be done on multidimensional arrays.

### Note

The general multidimensional array functions reside in the directory.

### Creating Multidimensional Arrays

You can use the same techniques to create multidimensional arrays that you use for two-dimensional matrices. In addition, MATLAB provides a special concatenation function that is useful for building multidimensional arrays.

This section discusses

#### Creating and Extending Multidimensional Arrays Using Indexed Assignment

You can create a multidimensional array by creating a 2-D array and extending it. Create a 2-D array and extend to a 3-D array using indexed assignment:

You can extend an array by assigning a single value to the new elements. MATLAB expands the scalar value to match the dimensions of the addressed elements. This expansion is called scalar expansion.

Extend by a third page using scalar expansion.

To extend the rows, columns, or pages of an array, use similar assignment statements. The dimensions of arrays on the right side and the left side of the assignment must be the same.

Extend into a 3-by-3-by-3-by-2, four-dimensional array. In the first assignment, MATLAB pads to fill the unassigned elements in the extended dimension with zeros. The second and third assignments replace the zeros with the specified values.

#### Generating Arrays Using MATLAB Functions

You can use MATLAB functions such as , , and to generate multidimensional arrays in the same way you use them for two-dimensional arrays. Each argument you supply represents the size of the corresponding dimension in the resulting array. For example, to create a 4-by-3-by-2 array of normally distributed random numbers:

To generate an array filled with a single constant value, use the function. replicates an array (in this case, a 1-by-1 array) through a vector of array dimensions.

### Note

Any dimension of an array can have size zero, making it a form of empty array. For example, 10-by-0-by-20 is a valid size for a multidimensional array.

#### Building Multidimensional Arrays with the cat Function

The function is a simple way to build multidimensional arrays; it concatenates a list of arrays along a specified dimension:

where , , and so on are the arrays to concatenate, and is the dimension along which to concatenate the arrays.

For example, to create a new array with :

The function accepts any combination of existing and new data. In addition, you can nest calls to . The lines below, for example, create a four-dimensional array.

automatically adds subscripts of 1 between dimensions, if necessary. For example, to create a 2-by-2-by-1-by-2 array, enter

In the previous case, inserts as many singleton dimensions as needed to create a four-dimensional array whose last dimension is not a singleton dimension. If the argument had been , the previous statement would have produced a 2-by-2-by-1-by-1-by-2 array. This adds additional s to indexing expressions for the array. To access the value in the four-dimensional case, use

### Accessing Multidimensional Array Properties

You can use the following MATLAB functions to get information about multidimensional arrays you have created.

— Returns the size of each array dimension.

size(C) ans = 2 2 1 2 rows columns dim3 dim4— Returns the number of dimensions in the array.

— Provides information on the format and storage of the array.

whos Name Size Bytes Class A 2x2x2 64 double array B 2x2x2 64 double array C 4-D 64 double array D 4-D 192 double array Grand total is 48 elements using 384 bytes

### Indexing Multidimensional Arrays

Many of the concepts that apply to two-dimensional matrices extend to multidimensional arrays as well.

To access a single element of a multidimensional array, use integer subscripts. Each subscript indexes a dimension—the first indexes the row dimension, the second indexes the column dimension, the third indexes the first page dimension, and so on.

Consider a 10-by-5-by-3 array of random integers:

To access element on page 2 of , for example, use .

You can use vectors as array subscripts. In this case, each vector element must be a valid subscript, that is, within the bounds defined by the dimensions of the array. To access elements , , and on page 3 of , use

#### The Colon and Multidimensional Array Indexing

The MATLAB colon indexing extends to multidimensional arrays. For example, to access the entire third column on page 2 of , use .

The colon operator is also useful for accessing other subsets of data. For example, results in a 2-by-2 array, a subset of the data on page 1 of . This matrix consists of the data in rows 2 and 3, columns 2 and 3, on the first page of the array.

The colon operator can appear as an array subscript on both sides of an assignment statement. For example, to create a 4-by-4 array of zeros:

Now assign a 2-by-2 subset of array to the four elements in the center of .

#### Linear Indexing with Multidimensional Arrays

MATLAB linear indexing also extends to multidimensional arrays. In this case, MATLAB operates on a page-by-page basis to create the storage column, again appending elements columnwise. See Linear Indexing for an introduction to this topic.

For example, consider a 5-by-4-by-3-by-2 array .

Again, a single subscript indexes directly into this column. For example, produces the result

If you specify two subscripts indicating row-column indices, MATLAB calculates the offset as described above. Two subscripts always access the first page of a multidimensional array, provided they are within the range of the original array dimensions.

If more than one subscript is present, all subscripts must conform to the original array dimensions. For example, is invalid because all pages of have only five rows.

If you specify more than two subscripts, MATLAB extends its indexing scheme accordingly. For example, consider four subscripts into a four-dimensional array with size . MATLAB calculates the offset into the storage column by

For example, if you index the array using subscripts (3, 4, 2, 1), MATLAB returns the value 5 (index 38 in the storage column).

In general, the offset formula for an array with dimensions _{1}_{2}_{3}_{n} using any subscripts is

*s*

_{n}

*-1*)(

*d*

_{n-1})(

*d*

_{n-2})

*...*(

*d*

_{1})

*+*(

*s*

_{n-1}

*-1*)(

*d*

_{n-2})

*...*(

*d*

_{1})

*+...+*(

*s*

_{2}

*-1*)(

*d*

_{1})

*+s*

_{1 }

Because of this scheme, you can index an array using any number of subscripts. You can append any number of s to the subscript list because these terms become zero. For example,

is equivalent to

#### Avoiding Ambiguity in Multidimensional Indexing

Some assignment statements, such as

are ambiguous because they do not provide enough information about the shape of the dimension to receive the data. In the case above, the statement tries to assign a one-dimensional vector to a two-dimensional destination. MATLAB produces an error for such cases. To resolve the ambiguity, be sure you provide enough information about the destination for the assigned data, and that both data and destination have the same shape. For example:

### Reshaping Multidimensional Arrays

Unless you change its shape or size, a MATLAB array retains the dimensions specified at its creation. You change array size by adding or deleting elements. You change array shape by re-specifying the array's row, column, or page dimensions while retaining the same elements. The function performs the latter operation. For multidimensional arrays, its form is

, , and so on represent the desired size for each dimension of the reshaped matrix. Note that a reshaped array must have the same number of elements as the original array (that is, the product of the dimension sizes is constant).

M | reshape(M, [6 5]) |
---|---|

The function operates in a columnwise manner. It creates the reshaped matrix by taking consecutive elements down each column of the original data construct.

C | reshape(C, [6 2]) |
---|---|

Here are several new arrays from reshaping :

#### Removing Singleton Dimensions

MATLAB automatically removes trailing singleton dimensions (dimensions whose sizes are equal to 1) from all multidimensional arrays. For example, if you attempt to create an array of size 3-by-2-by-1-by-1, perhaps with the command , then MATLAB strips away the singleton dimensions at the end of the array and creates a 3-by-2 matrix. This is because every array technically has an *infinite* number of trailing singleton dimensions. A 3-by-2 array is the same as an array with size 3-by-2-by-1-by-1-by-1-by-...

For example, consider the following 2-by-2 matrix, .

is a 2-by-2 identity matrix.

Find the size of the fourth dimension of .

Although is a 2-by-2 matrix, the size of the fourth dimension in is 1. In fact, the size of each dimension beyond the second is 1.

The first two dimensions of an array are never stripped away, since they are always relevant.

Even a scalar in MATLAB is a 1-by-1 matrix.

MATLAB creates singleton dimensions if you explicitly specify them when you create or reshape an array, or if you perform a calculation that results in an array dimension of one:

The function removes singleton dimensions from an array:

The function does not affect two-dimensional arrays; row vectors remain rows.

### Permuting Array Dimensions

The function reorders the dimensions of an array:

is a vector specifying the new order for the dimensions of , where 1 corresponds to the first dimension (rows), 2 corresponds to the second dimension (columns), 3 corresponds to pages, and so on.

For a more detailed look at the function, consider a four-dimensional array of size 5-by-4-by-3-by-2. Rearrange the dimensions, placing the column dimension first, followed by the second page dimension, the first page dimension, then the row dimension. The result is a 4-by-2-by-3-by-5 array.

You can think of 's operation as an extension of the function, which switches the row and column dimensions of a matrix. For , the order of the input dimension list determines the reordering of the subscripts. In the example above, element of becomes element of , element of becomes element of , and so on.

#### Inverse Permutation

The function is the inverse of . Given an input array and a vector of dimensions , produces an array such that returns .

For example, these statements create an array that is equal to the input array :

You can obtain the original array after permuting it by calling with the same vector of dimensions.

### Computing with Multidimensional Arrays

Many of the MATLAB computational and mathematical functions accept multidimensional arrays as arguments. These functions operate on specific dimensions of multidimensional arrays; that is, they operate on individual elements, on vectors, or on matrices.

#### Operating on Vectors

Functions that operate on vectors, like , , and so on, by default typically work on the first nonsingleton dimension of a multidimensional array. Most of these functions optionally let you specify a particular dimension on which to operate. There are exceptions, however. For example, the function, which finds the cross product of two vectors, works on the first nonsingleton dimension having length 3.

### Note

In many cases, these functions have other restrictions on the input arguments — for example, some functions that accept multiple arrays require that the arrays be the same size. Refer to the online help for details on function arguments.

#### Operating Element-by-Element

MATLAB functions that operate element-by-element on two-dimensional arrays, like the trigonometric and exponential functions in the directory, work in exactly the same way for multidimensional cases. For example, the function returns an array the same size as the function's input argument. Each element of the output array is the sine of the corresponding element of the input array.

Similarly, the arithmetic, logical, and relational operators all work with corresponding elements of multidimensional arrays that are the same size in every dimension. If one operand is a scalar and one an array, the operator applies the scalar to each element of the array.

#### Operating on Planes and Matrices

Functions that operate on planes or matrices, such as the linear algebra and matrix functions in the directory, do not accept multidimensional arrays as arguments. That is, you cannot use the functions in the directory, or the array operators , , , or , with multidimensional arguments. Supplying multidimensional arguments or operands in these cases results in an error.

You can use indexing to apply a matrix function or operator to matrices within a multidimensional array. For example, create a three-dimensional array :

Applying the function to the entire multidimensional array results in an error:

You can, however, apply to planes within the array. For example, use colon notation to index just one page (in this case, the second) of the array:

### Note

In the first case, subscripts are not colons; you must use to avoid an error. For example, results in an error because the size of the input is . The expression , however, passes a valid two-dimensional matrix to .

### Organizing Data in Multidimensional Arrays

You can use multidimensional arrays to represent data in two ways:

As planes or pages of two-dimensional data. You can then treat these pages as matrices.

As multivariate or multidimensional data. For example, you might have a four-dimensional array where each element corresponds to either a temperature or air pressure measurement taken at one of a set of equally spaced points in a room.

For example, consider an RGB image. For a single image, a multidimensional array is probably the easiest way to store and access data.

To access an entire plane of the image, use

To access a subimage, use

The RGB image is a good example of data that needs to be accessed in planes for operations like display or filtering. In other instances, however, the data itself might be multidimensional. For example, consider a set of temperature measurements taken at equally spaced points in a room. Here the location of each value is an integral part of the data set—the physical placement in three-space of each element is an aspect of the information. Such data also lends itself to representation as a multidimensional array.

Now to find the average of all the measurements, use

To obtain a vector of the “middle” values (element (2,2)) in the room on each page, use

### Multidimensional Cell Arrays

Like numeric arrays, the framework for multidimensional cell arrays in MATLAB is an extension of the two-dimensional cell array model. You can use the function to build multidimensional cell arrays, just as you use it for numeric arrays.

For example, create a simple three-dimensional cell array :

The subscripts for the cells of look like

### Multidimensional Structure Arrays

Multidimensional structure arrays are extensions of rectangular structure arrays. Like other types of multidimensional arrays, you can build them using direct assignment or the function:

#### Applying Functions to Multidimensional Structure Arrays

To apply functions to multidimensional structure arrays, operate on fields and field elements using indexing. For example, find the sum of the columns of the array in :

Similarly, add all the fields in the array:

## 0 Replies to “Assignment Operator Matlab Matrix”