- An array is a set of numbers, characters, or logical values organized as a rectangular form.
- An array can be a scalar, vector, matrix, or multi-dimensional.
- For an array, the length of its k-th dimension is the number of elements along that dimension.
- For examples, for each of the following arrays, the length of the second dimension is 2:
ones(3,2,1) [1 2;3 4] zeros(1,2,3,4)
- A scalar is an array having only one number, character, or logical value (i.e., all of its dimensions have length 1).
- The following are examples of scalar:
ones(1,1,1,1) zeros(1,1) 1 -0.05  'a' '?' true false cos(90) 2-0.4
- A vector can be a row vector or a column vector.
- A row vector is an array that each of its dimensions has unit length, except the second dimension.
- The following are examples of row vectors:
'abcd1234' [true false true false] zeros(1,3,1,1,1) [1 2 3] linspace(1,2) 2:2:20
- A column vector is an array that each of its dimensions has unit length, except the first dimension.
- The following are examples of column vectors:
['abcd1234']' [true false true false]' zeros(3,1,1,1) [1; 2; 3] linspace(1,2)' (2:2:20)'
- A matrix is an array that each of its dimensions has length 1 except the first two dimensions.
- The following are examples of matrices:
ones(4,5,1,1) diag(1:10) ['abc'; 'def'] [1 2;3 4]
An array is said to be multi-dimensional if its k-th dimension, where k > 2, has length greater than 1. The following are examples of multi-dimensional arrays:
ones(1,1,4,5) true(3,3,3,3,1,1,1) zeros(3,4,3,4,1,1)
- The size of an array can be obtained by the function
- For a scalar, its size is the vector
- For a row vector, its size is
[1 s], where
sis the vector's length.
- For a column vector, its size is
[s 1], where
sis the vector's length.
- For a matrix, its size is
[s1 s2], where
s2are the lengths of its 1st and 2nd dimensions, respectively.
- For a multi-dimensional array, its size is the row vector of elements
sn, representing the lengths of the array's dimensions. Here
nis greater than 2, and is the highest dimension with length greater than 1.
Number of Dimensions
- The number of dimensions of an array is the length of its size vector.
- If a dimension of an array has length 1, it is called a singleton dimension.
- A non-singleton dimension is a dimension of length > 1.
- The first non-singleton dimension is a non-singleton dimension with the smallest dimension number.
- For example, the first dimension of
ones(1,1,3,4)is singleton, while the first non-singleton dimension is 3.
- Some built-in functions and operators perform elementwise operations of its input arguments, for examples,
- For these functions or operators, the input arguments are expected to have the same sizes. For example, in
Bare expected to have the same sizes.
- However, if an argument is a scalar whereas the other argument is an array, the scalar would be expanded to match the size of the array.
- For examples, each of the following statements performs elementwise operations on a constant array and a randomly generated 3-by-3 array:
1 + rand(3) 0.8 & rand(3) poissinv(0.2, randi(3,3))
- In the first statement above, 1 would be expanded into a 3-by-3 array of all 1s before added to
- For some functions, if two arguments are vectors of the same lengths, they need not be both row vectors or both column vectors. In fact, one of them can be a row vector and the other a column vector.