Element-Wise Operations
You often may want to perform an operation on each element of a vector
while doing a computation. For example, you may want to add two vectors
by adding all of the corresponding elements. The addition (+) and
subtraction (-) operators are defined to work on matrices as well as
scalars. For example, if x = [1 2 3] and y = [5 6 2], then
>> w = x+y
w =
6 8 5
Multiplying two matrices element by element is a little different. The *
symbol is defined as matrix multiplication when used on two matrices.
Use .* to specify element-wise multiplication. So, using the x and y
from above,
>> w = x .* y
w =
5 12 6
You can perform exponentiation on a vector similarly. Typing x .^ 2
squares each element of x.
>> w = x .^ 2
w =
1 4 9
Finally, you cannot use / to divide two matrices element-wise, since /
and \ are reserved for left and right matrix ``division.'' Instead, you
must use the ./ function. For example:
>> w = y ./ x
w =
5.0000 3.0000 0.6667
All of these operations work for complex numbers as well.
The abs operator returns the magnitude of its argument. If applied to a
vector, it returns a vector of the magnitudes of the elements. For
example, if x = [2 -4 3-4*i -3*i]:
>> y = abs(x)
y =
2 4 5 3
The angle operator returns the phase angle (i.e., the "argument") of its
operand in radians. The angle operator can also work element-wise
across a vector. For example:
>> phase = angle(x)
phase=
0 3.1416 -0.9273 -1.5708
The sqrt function computes the square root of its argument. If its
argument is a matrix or vector, it computes the square root of each
element. For example:
>> x
x =
4 -9 i 2-2*i
>> y = sqrt(x)
y =
2.0000 0 + 3.0000i 0.7071 + 0.7071i 1.5538 - 0.6436i
MATLAB also has operators for taking the real part, imaginary part, or
complex conjugate of a complex number. These functions are real, imag
and conj, respectively. They are defined to work element-wise on any
matrix or vector.
MATLAB has several operators that round fractional numbers to integers.
The round function rounds its argument to the nearest integer. The fix
function rounds its argument to the nearest integer towards zero, e.g.
rounds ``down'' for positive numbers, and ``up'' for negative numbers.
The floor function rounds its argument to the nearest integer towards
negative infinity, e.g. ``down.'' The ceil (short for ceiling) function
rounds its argument to the nearest integer towards positive infinity,
e.g. ``up.''
round rounds to nearest integer
fix rounds to nearest integer towards zero
floor rounds down (towards negative infinity)
ceil rounds up (towards positive infinity)
All of these commands are defined to work element-wise on matrices and
vectors. If you apply one of them to a complex number, it will round
both the real and imaginary part in the manner indicated. For example:
>> ceil(3.1+2.4*i)
ans=
4.0000 + 3.0000i
MATLAB can also calculate the remainder of an integer division
operation. If x = y * n + r, where n is an integer and r is less than n
but is not negative, then rem(x,y) is r. For example:
>> x
x=
8 5 11
>> y
y=
6 5 3
>> r = rem(x,y)
r=
2 0 2
The standard trigonometric operations are all defined as element-wise
operators. The operators sin, cos and tan calculate the sine, cosine
and tangent of their arguments. The arguments to these functions are
angles in radians. Note that the functions are also defined on complex
arguments. For example, cos(x+iy) = cos(x)cosh(y) - i sin(x)sinh(y).
The inverse trig functions (acos, asin and atan) are also defined to
operate element-wise across matrices. Again, these are defined on
complex numbers, which can lead to problems for the incautious user. The
arctangent is defined to return angles between pi/2 and -pi/2.
In addition to the primary interval arctangent discussed above, MATLAB
has a full four-quadrant arctangent operator, atan2. atan2(y,x) returns
the angle between -pi and pi whose tangent is the real part of y/x. If
x and y are vectors, atan2(y,x) divides y by x element-wise, then
returns a vector in which each element is the four-quadrant arctangent
of corresponding element of the y/x vector.
MATLAB also includes functions for exponentials and logarithms. The exp
operator computes e to the power of its argument. This works
element-wise, and on complex numbers. So, to generate the complex
exponential with a frequency of pi/4, we could type:
>> n = 0:7;
>> s = exp(i*(pi/4)*n)
s =
Columns 1 through 4
1.00 0.7071+0.7071i 0.00+1.0000i -0.7071+0.7071i
Columns 5 through 8
-1.0000 + 0.0000i -0.7071 - 0.7071i -0.0000 - 1.0000i 0.7071 - 0.7071i
MATLAB also has natural and base-10 logarithms. The log function
calculates natural logs, and log10 calculates base-10 logs. Both
operate element-wise for vectors. Both are defined for complex values.