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:0gives the row
x = 1.0000 0.8000 0.6000 0.4000 0.2000 0If the difference between consecutive elements of the row is
>> x = 1:5gives the row
x = 1 2 3 4 5
For arrays with
Such an array can be entered in different ways:
>> a = [1 2 3;4 5 6;7 8 9]or
>> a = [1,2,3;4,5,6;7,8,9]
>> a = [1 2 3 4 5 6 7 8 9]
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.

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
>> [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 7and subtraction of the arrays results in
>> ab ans = 5 3 1Note that the arrays
a
and b
should be of the same size.
Array operations, such as multiplication `*', division `/' and raising to a power `
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 81For all these operations, the following conventions hold:
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 9Detailed information about these operations can be obtained with the command
help arith
.
Consider the function
We want to assign the row vector
>> 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.0000For making the row of function values, we use array operations. Here, using dots is necessary, since the function
>> 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.6000In 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
>> A > B ans = 0 1 1 1 1 0Entries with a `1' (true) indicate the elements of
>> A > 2gives:
ans = 1 1 1 1 1 0The relational operations are given in the Table 1.6.

find
. Look at the result of the command help find
.
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.7321Consider the function
>> x = 0:0.25:4*pi; >> y = x.*sin(x)The result is an array with the corresponding 51 function values.
>> [nr,nc] = size(A)The result gives the size of
nr
and the number of columns nc
.
The function
>> sum(A)calculates the column sums in a rectangular array
>> prod(A)does the same for the product.
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 6transposition proceeds as follows:
>> transpose(a) ans = 1 2 3and
>> transpose(A) ans = 1 3 5 2 4 6Transposition 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.