Multidimensional Arrays - MATLAB & Simulink - MathWorks India
Multidimensional Arrays - MATLAB & Simulink - MathWorks India
Multidimensional Arrays
On this page
Overview Creating Multidimensional Arrays Accessing Multidimensional Array Properties Indexing Multidimensional Arrays Reshaping Multidimensional Arrays Permuting Array Dimensions Computing w ith Multidimensional Arrays Organizing Data in Multidimensional Arrays Multidimensional Cell Arrays Multidimensional Structure Arrays
Overview
An array having more than tw o dimensions is called a multidimensional array in the MATLAB application. Multidimensional arrays in MATLAB are an extension of the normal tw o-dimensional matrix. Matrices have tw o dimensions: the row dimension and the column dimension.
You can access a tw o-dimensional matrix element w ith tw o 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 ( 2 , 3 , 2 ) .
http://www.mathworks.in/help/matlab/math/multidimensional-arrays.html
1/10
1/17/2014
As you add dimensions to an array, you also add subscripts. A four-dimensional array, for example, has four subscripts. The first tw o reference a row -column pair; the second tw o access the third and fourth dimensions of data. Most of the operations that you can perform on matrices (i.e., tw o-dimensional arrays) can also be done on multidimensional arrays. Note The general multidimensional array functions reside in the d a t a t y p e sdirectory.
8 9 6
4 6 7
You can continue to add row s, columns, or pages to the array using similar assignment statements. Extending Multidim ensional Arrays To extend Ain any dimension: Increment or add the appropriate subscript and assign the desired values. Assign the same number of elements to corresponding array dimensions. For numeric arrays, all row s must have the same number of elements, all pages must have the same number of row s and columns, and so on. You can take advantage of the MATLAB scalar expansion capabilities, together w ith the colon operator, to fill an entire dimension w ith a single value: A ( : , : , 3 )=5 ;
http://www.mathworks.in/help/matlab/math/multidimensional-arrays.html 2/10
1/17/2014
A ( : , : , 3 ) a n s= 5 5 5
5 5 5
5 5 5
To turn Ainto a 3-by-3-by-3-by-2, four-dimensional array, enter A ( : , : , 1 , 2 )=[ 123 ;456 ;789 ] ; A ( : , : , 2 , 2 )=[ 987 ;654 ;321 ] ; A ( : , : , 3 , 2 )=[ 101 ;110 ;011 ] ; Note that after the first tw o assignments MATLAB pads Aw ith zeros, as needed, to maintain the corresponding sizes of dimensions. Generating Arrays Using MATLAB Functions You can use MATLAB functions such as r a n d n ,o n e s , and z e r o sto generate multidimensional arrays in the same w ay you use them for tw o-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: B=r a n d n ( 4 , 3 , 2 ) To generate an array filled w ith a single constant value, use the r e p m a tfunction. r e p m a treplicates an array (in this case, a 1-by-1 array) through a vector of array dimensions. B=r e p m a t ( 5 ,[ 342 ] ) B ( : , : , 1 )= 5 5 5 5 5 5 B ( : , : , 2 )= 5 5 5 5 5 5
5 5 5
5 5 5
5 5 5
5 5 5
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 Multidim ensional Arrays w ith the cat Function The c a tfunction is a simple w ay to build multidimensional arrays; it concatenates a list of arrays along a specified dimension: B=c a t ( d i m ,A 1 ,A 2 . . . ) w here A 1 ,A 2 , and so on are the arrays to concatenate, and d i mis the dimension along w hich to concatenate the arrays. For example, to create a new array w ith c a t : B=c a t ( 3 ,[ 28 ;05 ] ,[ 13 ;79 ] ) B ( : , : , 1 )= 2 8 0 5 B ( : , : , 2 )= 1 3 7 9 The c a tfunction accepts any combination of existing and new data. In addition, you can nest calls to c a t . The lines below , for example, create a four-dimensional array. A=c a t ( 3 ,[ 92 ;65 ] ,[ 71 ;84 ] ) B=c a t ( 3 ,[ 35 ;01 ] ,[ 56 ;21 ] ) D=c a t ( 4 ,A ,B ,c a t ( 3 ,[ 12 ;34 ] ,[ 43 ; 21 ] ) ) c a tautomatically adds subscripts of 1 betw een dimensions, if necessary. For example, to create a 2-by-2-by-1-by-2 array,
http://www.mathworks.in/help/matlab/math/multidimensional-arrays.html 3/10
1/17/2014
enter C=c a t ( 4 ,[ 12 ;45 ] ,[ 78 ;32 ] ) In the previous case, c a tinserts as many singleton dimensions as needed to create a four-dimensional array w hose last dimension is not a singleton dimension. If the d i margument had been 5 , the previous statement w ould have produced a 2-by2-by-1-by-1-by-2 array. This adds additional 1 s to indexing expressions for the array. To access the value 8in the fourdimensional case, use
2 d i m 4
n d i m s Returns the number of dimensions in the array. n d i m s ( C ) a n s= 4 w h o s Provides information on the format and storage of the array. w h o s N a m e A B C D S i z e 2 x 2 x 2 2 x 2 x 2 4 D 4 D B y t e s 6 4 6 4 6 4 1 9 2 C l a s s d o u b l ea r r a y d o u b l ea r r a y d o u b l ea r r a y d o u b l ea r r a y
G r a n dt o t a li s4 8e l e m e n t su s i n g3 8 4b y t e s
1/17/2014
Now assign a 2-by-2 subset of array n d d a t ato the four elements in the center of C . C ( 2 : 3 , 2 : 3 )=n d d a t a ( 2 : 3 , 1 : 2 , 2 ) Linear Indexing w ith Multidim ensional 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 columnw ise. See Linear Indexing for an introduction to this topic. For example, consider a 5-by-4-by-3-by-2 array C .
Again, a single subscript indexes directly into this column. For example, C ( 4 )produces the result a n s= 0 If you specify tw o subscripts ( i , j )indicating row -column indices, MATLAB calculates the offset as described above. Tw o subscripts alw ays access the first page of a multidimensional array, provided they are w ithin 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, C ( 6 , 2 )is invalid because all pages of Chave only five row s. If you specify more than tw o subscripts, MATLAB extends its indexing scheme accordingly. For example, consider four subscripts ( i , j , k , l )into a four-dimensional array w ith size [ d 1d 2d 3d 4 ] . MATLAB calculates the offset into the storage column by ( l 1 ) ( d 3 ) ( d 2 ) ( d 1 ) + ( k 1 ) ( d 2 ) ( d 1 ) + ( j 1 ) ( d 1 ) + i For example, if you index the array Cusing subscripts (3, 4, 2, 1), MATLAB returns the value 5 (index 38 in the storage column). In general, the offset formula for an array w ith dimensions [ d . .d s . .s 1 d 2 d 3 . n]using any subscripts ( 1 s 2 s 3 . n) is ( s 1 ) ( d ) ( d ) . . . ( d ) + ( s 1 ) ( d ) . . . ( d ) + . . . + ( s 1 ) ( d ) + s n n 1 n 2 1 n 1 n 2 1 2 1 1 Because of this scheme, you can index an array using any number of subscripts. You can append any number of 1 s to the subscript list because these terms become zero. For example, C ( 3 , 2 , 1 , 1 , 1 , 1 , 1 , 1 ) is equivalent to C ( 3 , 2 ) Avoiding Am biguity in Multidim ensional Indexing
http://www.mathworks.in/help/matlab/math/multidimensional-arrays.html 5/10
1/17/2014
Some assignment statements, such as A ( : , : , 2 )=1 : 1 0 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 tw o-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: A ( 1 , : , 2 )=1 : 1 0 ;
The r e s h a p efunction operates in a columnw ise manner. It creates the reshaped matrix by taking consecutive elements dow n each column of the original data construct.
C reshape(C, [6 2])
Here are several new arrays from reshaping n d d a t a : B=r e s h a p e ( n d d a t a ,[ 62 5 ] ) C=r e s h a p e ( n d d a t a ,[ 531 0 ] ) D=r e s h a p e ( n d d a t a ,[ 5325 ] ) Rem oving Singleton Dim ensions MATLAB creates singleton dimensions if you explicitly specify them w hen you create or reshape an array, or if you perform a calculation that results in an array dimension of one: B=r e p m a t ( 5 ,[ 2314 ] ) ; s i z e ( B ) a n s= 2
4
6/10
http://www.mathworks.in/help/matlab/math/multidimensional-arrays.html
1/17/2014
The s q u e e z efunction does not affect tw o-dimensional arrays; row vectors remain row s.
For a more detailed look at the p e r m u t efunction, consider a four-dimensional array Aof size 5-by-4-by-3-by-2. Rearrange the dimensions, placing the column dimension first, follow ed 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 p e r m u t e 's operation as an extension of the t r a n s p o s efunction, w hich sw itches the row and column dimensions of a matrix. For p e r m u t e , the order of the input dimension list determines the reordering of the subscripts. In the example above, element ( 4 , 2 , 1 , 2 )of Abecomes element ( 2 , 2 , 1 , 4 )of B , element ( 5 , 4 , 3 , 2 )of Abecomes element ( 4 , 2 , 3 , 5 )of B , and so on. Inverse Perm utation The i p e r m u t efunction is the inverse of p e r m u t e . Given an input array Aand a vector of dimensions v ,i p e r m u t e produces an array Bsuch that p e r m u t e ( B , v )returns A . For example, these statements create an array Ethat is equal to the input array C : D=i p e r m u t e ( C ,[ 1423 ] ) ; E=p e r m u t e ( D ,[ 1423 ] ) You can obtain the original array after permuting it by calling i p e r m u t ew ith the same vector of dimensions.
1/17/2014
Functions that operate on vectors, like s u m ,m e a n , and so on, by default typically w ork on the first nonsingleton dimension of a multidimensional array. Most of these functions optionally let you specify a particular dimension on w hich to operate. There are exceptions, how ever. For example, the c r o s sfunction, w hich finds the cross product of tw o vectors, w orks 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 Elem ent-by-Elem ent MATLAB functions that operate element-by-element on tw o-dimensional arrays, like the trigonometric and exponential functions in the e l f u ndirectory, w ork in exactly the same w ay for multidimensional cases. For example, the s i nfunction 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 w ork w ith 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 m a t f u ndirectory, do not accept multidimensional arrays as arguments. That is, you cannot use the functions in the m a t f u ndirectory, or the array operators *, ^ ,\ , or / , w ith 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 w ithin a multidimensional array. For example, create a three-dimensional array A : A=c a t ( 3 ,[ 123 ;987 ;465 ] ,[ 032 ;884 ;535 ] ,. . . [ 647 ;685 ;543 ] ) ; Applying the e i gfunction to the entire multidimensional array results in an error: e i g ( A ) ? ? ?U n d e f i n e df u n c t i o no rm e t h o d' e i g 'f o ri n p u t a r g u m e n t so ft y p e' d o u b l e 'a n da t t r i b u t e s' f u l l3 dr e a l ' . You can, how ever, apply e i gto planes w ithin the array. For example, use colon notation to index just one page (in this case, the second) of the array: e i g ( A ( : , : , 2 ) ) a n s= 1 2 . 9 1 2 9 2 . 6 2 6 0 2 . 7 1 3 1 Note In the first case, subscripts are not colons; you must use s q u e e z eto avoid an error. For example, e i g ( A ( 2 , : , : ) )results in an error because the size of the input is [ 133 ] . The expression e i g ( s q u e e z e ( A ( 2 , : , : ) ) ) , how ever, passes a valid tw o-dimensional matrix to e i g .
http://www.mathworks.in/help/matlab/math/multidimensional-arrays.html
8/10
1/17/2014
To access an entire plane of the image, use r e d P l a n e=R G B ( : , : , 1 ) ; To access a subimage, use s u b i m a g e=R G B ( 2 0 : 4 0 , 5 0 : 8 5 , : ) ; 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, how ever, 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 setthe 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 m e a n ( m e a n ( m e a n ( T E M P ) ) ) ; To obtain a vector of the "middle" values (element (2,2)) in the room on each page, use B=T E M P ( 2 , 2 , : ) ;
1/17/2014
B { 1 , 2 }=3 ; B { 2 , 1 }=0 : 1 : 3 ; B { 2 , 2 }=[ 45 ] ' ; C=c a t ( 3 ,A ,B ) ; The subscripts for the cells of Clook like
Applying Functions to Multidim ensional 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 t e s tarray in p a t i e n t ( 1 , 1 , 2 ) : s u m ( ( p a t i e n t ( 1 , 1 , 2 ) . t e s t ) ) ; Similarly, add all the b i l l i n gfields in the p a t i e n tarray: t o t a l=s u m ( [ p a t i e n t . b i l l i n g ] ) ;
http://www.mathworks.in/help/matlab/math/multidimensional-arrays.html
10/10