Arrays operation


1. Array Addition and Subtraction:


For example




Array subtraction is performed in a similar way. The addition shown in above equation is performed in MATLAB as follows:




2. Array multiplication:


Multiplying a matrix A by a scalar w produces a matrix whose elements are the elements of A multiplied by w. For example:




This multiplication is performed in MATLAB as follows:




Multiplication of an array by a scalar is easily defined and easily carried out. However, multiplication of two arrays is not so straightforward. 


MATLAB uses two definitions of multiplication:


a. array multiplication (also called element-by-element multiplication)



b. matrix multiplication.

MATLAB has two forms of arithmetic operations on arrays. One form, called array operations, which are also called element-by-element operations. And matrix operations. Each form has its own applications.



Division and exponentiation must also be carefully defined when you are dealing with operations between two arrays.




Array or Element-by-element multiplication is defined only for arrays having the same size. The definition of the product x.*y, where x and y each have n elements, is 

x.*y = [x(1)y(1), x(2)y(2), ... , x(n)y(n)] 
if x and y are row vectors. 

For example, 




If x and y are column vectors, the result of x.*y is a column vector. For example z = (x').*(y') gives 




Note that x' is a column vector with size 3 × 1 and thus does not have the same size as y, whose size is 1 × 3. Thus for the vectors x and y the operations x'.*y and y.*x' are not defined in MATLAB and will generate an error message.


The array operations are performed between the elements in corresponding locations in the arrays. For example, the array multiplication operation A.*B results in a matrix C that has the same size as A and B and has the elements ci j = ai j bi j . For example,




The built-in MATLAB functions such as sqrt(x) and exp(x) automatically operate on array arguments to produce an array result the same size as the array argument x.

Thus these functions are said to be vectorized functions.

For example, in the following session the result y has the same size as the argument x.



However, when multiplying or dividing these functions, or when raising them to a power, you must use element-by-element operations if the arguments are arrays.

For example, to compute 
z = (ey sin x) cos2x, 
you must type
z = exp(y).*sin(x).*(cos(x)).^2.

You will get an error message if the size of x is not the same as the size of y. The result z will have the same size as x and y.


3. Array division:


The definition of array division is similar to the definition of array multiplication except that the elements of one array are divided by the elements of the other array. Both arrays must have the same size. The symbol for array right division is ./ 

For example, if

x = [8, 12, 15] y = [–2, 6, 5]


then z = x./y gives



z = [8/(–2), 12/6, 15/5] = [–4, 2, 3]



4. Array exponentiation:


MATLAB enables us not only to raise arrays to powers but also to raise scalars and arrays to array powers. To perform exponentiation on an element-by-element basis, we must use the ( .^ ) symbol. For example, 

if x = [3, 5, 8], then typing x.^3 produces the array [33, 53, 83] = [27, 125, 512].

We can raise a scalar to an array power. For example, 

if p = [2, 4, 5], then typing 3.^p produces the array [32, 34, 35] = [9, 81, 243].


Remember that ( .^ ) is a single symbol. The dot in 3.^p is not a decimal point associated with the number 3. The following operations, with the value of p given here, are equivalent and give the correct answer:


>> 3.^p


>> 3.0.^p


>> 3..^p


>> (3).^p


>> 3.^[2,4,5]