# Arrays

MATLAB uses matrices (or arrays) as the basic calculation unit. An array is a rectangular scheme of numbers, called elements, arranged in rows and columns. The simplest array contains only one column or one row.

An array with one row is also called a row or a row vector, and an array with one column is also called a column or a column vector. If the difference between consecutive elements in a row is always the same, the row can be formed with the command:

variable' = initial value':step size':final value'

Here initial value' is the first element of the row, final value' is the last element of the row, and step size' is the difference between the consecutive elements. For example:

>> x = 1:-0.2:0

gives the row
x =
1.0000    0.8000    0.6000    0.4000    0.2000         0

If the difference between consecutive elements of the row is , step size' can be omitted. For example,
>> x = 1:5

gives the row
x =
1     2     3     4     5


For arrays with rows and columns, the numbers or elements are sorted in different rows and columns:

Such an array can be entered in different ways:

• Between [' and ]' the elements are separated by spaces or commas, and the rows are separated by a semicolon. The commands are:
>> a = [1 2 3;4 5 6;7 8 9]

or
>> a = [1,2,3;4,5,6;7,8,9]

• Between [' and ]' where the elements are separated by spaces or commas, and each row is entered on a new line by using the return' key. The command now is
>> a = [1 2 3
4 5 6
7 8 9]

In each of the cases above, the result is
a =
1     2     3
4     5     6
7     8     9


Elements of arrays can be indicated by means of their index. In the case of row and column vectors, one index suffices. This can be used according to the Table 1.5.

Table 1.5: Using array indices
MATLAB command Result
a(1,2) Gives the element on the first row and second column of .
a(1,:) Gives the first row of .
a(:,3) Gives the third column of .
a(:,2)=[10;10;10] Changes the second column of into a column with 10's only. Only works if a is a array,
b(1) the first element of in case contains either one row or one column.
a(1,[3,1]) an array consisting of the and elements of the first row of .

Arrays (both matrices and vectors) can be concatenated into new arrays. If the arrays are arranged in a row, the number of rows in the arrays have to be equal. If the arrays are arranged in column, the number of columns in the arrays have to be equal. The concatenation of the arrays and proceeds as follows:

>> [a,b]

ans =
2     3     4     6     5


Array operations

An array operation' is an operation (like addition or subtraction) that is applied to corresponding elements of two arrays of the same form. When an operation is applied to all elements of one array, one also speaks of an array operation'.

The operations addition and subtraction are automatically interpreted as array operations. After the assignments

>> a = [1,2,3]; b = [6,5,4];

addition of the arrays results in
>> a+b

ans =
7     7     7

and subtraction of the arrays results in
>> a-b

ans =
-5    -3    -1

Note that the arrays a and b should be of the same size.

Array operations, such as multiplication *', division /' and raising to a power  ', are indicated by putting a dot .' in front of the operation sign. In these array operations, the operation is performed elementwise, e.g. the 1,1-element of a.*b, yields a(1,1)*b(1,1) when a and b are arrays of appropriate sizes. Hence

>> a.*b

ans =
6    10    12

>> a./b

ans =
0.1667    0.4000    0.7500

>> a.^b

ans =
1    32    81

For all these operations, the following conventions hold:

1. If in an array operation one of the arrays is a number, this number is interpreted as an array of which each element equals this number, and of which the size is equal to the size of the other array.
2. If each element of an array is to be multiplied by a scalar, one does not need to put a dot in front of the multiplication sign.

Examples:

>> 2.^b

ans =
64    32    16

>> [2 2 2].^b

ans =
64    32    16

>> a./2

ans =
0.5000    1.0000    1.5000

>> 3*a

ans =
3     6     9

Detailed information about these operations can be obtained with the command help arith.

Producing an array of function values

Consider the function .
We want to assign the row vector to the variable y. First we make an array with the arguments [0, 0.2, , 1.8, 2].

>> x = 0:0.2:2

x =
Columns 1 through 7

0    0.2000    0.4000    0.6000    0.8000    1.0000    1.2000

Columns 8 through 11

1.4000    1.6000    1.8000    2.0000

For making the row of function values, we use array operations. Here, using dots is necessary, since the function should be evaluated per element of .
>> y = x.^3./(1+x.^2)

y =
Columns 1 through 7

0    0.0077    0.0552    0.1588    0.3122    0.5000    0.7082

Columns 8 through 11

0.9270    1.1506    1.3755    1.6000

In fact, this boils down to a componentwise division of a row of numerators and a row of denominators.

Remark: Multiplication of matrices will be discussed in Chapter 4.

Relational array operations

Relational array operations are operations where arrays are compared elementwise. For example, for the matrices

the command for finding out which elements in are larger than corresponding elements in is given by:
>> A > B

ans =
0     1     1
1     1     0

Entries with a 1' (true) indicate the elements of that are larger than corresponding elements of . Otherwise the entry will be 0' (false). The result is thus an array with boolean entries. You can only compare the elements of array and if the arrays and have the same size, or if one of the arrays is a scalar. For example,
>> A > 2

gives:
ans =
1     1     1
1     1     0

The relational operations are given in the Table 1.6.

Table 1.6: Relation operators
Operation Meaning
> greater than
>= greater than or equal to
< lower than
<= lower than or equal to
== equal to
~= not equal to

Relational operations are often used in combination with the function find. Look at the result of the command help find.

Other array operations

In MATLAB, the mathematical standard functions sin, cos, sqrt, atan, asin, etc. automatically operate on arrays.

>> a = [1 2 3];
>> sin(a)

ans =
0.8415    0.9093    0.1411

>> sqrt(a)

ans =
1.0000    1.4142    1.7321

Consider the function . A row of function values can be generated with the following command:
>> x = 0:0.25:4*pi;
>> y = x.*sin(x)

The result is an array with the corresponding 51 function values.

Some other operations are
>> [nr,nc] = size(A)

The result gives the size of , i.e., the number of rows nr and the number of columns nc. The function
>> sum(A)

calculates the column sums in a rectangular array and gives the sums in a row vector. (A column sum is the sum of the elements of a column.) If is a row vector, then the sum of the elements of is calculated. The function
>> prod(A)

does the same for the product.

Transposition

In an array (matrix), the rows and columns can be interchanged. This is called transposition. The result is called the transposed array. In MATLAB, transposition can be performed with the function transpose. After the commands

>> a = [1 2 3]

a =
1     2     3

>> A = [1 2;3 4;5 6]

A =
1     2
3     4
5     6

transposition proceeds as follows:
>> transpose(a)

ans =
1
2
3

and
>> transpose(A)

ans =
1     3     5
2     4     6

Transposition can also be performed by using an accent ('). The commands transpose(A) and A'` give the same result for arrays containing real valued elements, but not for matrices having complex elements. Additional information about this will be provided in Chapter 4.

Esteur 2010-03-22