Lesson: Vector Algebra (Algebra
with many numbers, all at once...)
||You'll learn to create arrays and vectors,
and how to perform algebra and trigonometric operations on them. This
is called Vector Algebra.
An array is an arbitrary list of numbers or expressions arranged in horizontal rows and vertical columns.
When an array has only one row or column, it is called a vector. An
array with m
rows and n
columns is a called a matrix of size m x n.
Launch MATLAB and reproduce the following information. You type only
what you see right after the '>>' sign. MATLAB confirms
what you enter, or gives an answer.
with 3 elements (spaces determine different
columns). Start your vectors with '['
and end them with ']'.
>> x=[3 4 5]
with 3 elements (use the ';'
sign to separate
each row). MATLAB confirms this column vector.
>> y=[3; 4; 5]
You can add or subtract vectors of
the same size:
You cannot add/subtract a row to/from a column (Matlab indicates the
??? Error using
You can multiply or divide element-by-element
of same-sized vectors
(using the '.*'
operators) and assign the result to a different
9 16 25
>> a=[1 2 3].*x
>> b=x./[7 6 5]
b = 0.4286
Multiplying (or dividing) a vector with (or by) a scalar does not need
any special operator (you can use just '*' or '/'):
>> c = 3*x
9 12 15
>> d = y/2
The instruction 'linspace'
creates a vector with some elements linearly
spaced between your initial and final specified
numbers, for example:
r = linspace(initial_number, final_number, number_of_elements)
>> r = linspace(2,6,5)
r = 2
>> r = linspace(2,3,4)
r = 2.0000
functions (sin, cos, tan...) and math
log, exp...) operate on vectors element-by-element (angles are in
ans = -0.4161
-0.6908 -0.8893 -0.9900
So far, so good?
Experimenting with numbers, vectors and matrices is good for you and it
does not hurt! Vector algebra can even be fun if you work with a
computer and a good software, can't it?
'Vector Algebra' to
Algebra' to Tutorials