Programming and Data Types | ![]() ![]() |
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
sum
, mean
, 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 cross
function, which finds the cross product of two vectors, works on the first nonsingleton dimension having length three.
Operating Element-by-Element
MATLAB functions that operate element-by-element on two-dimensional arrays, like the trigonometric and exponential functions in the elfun
directory, work in exactly the same way for multidimensional cases. For example, the sin
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.
![]() | Permuting Array Dimensions | Operating on Planes and Matrices | ![]() |