You are on page 1of 20

Vectors & Matrices

•Vectors and matrices are used to store sets of values, all of which are the
same type.
A vector can be either a row vector or a column vector.

x = [1 2 5 1] or
x =
1
2
5
1

If a vector has n elements, a row vector would have the dimensions 1 × n,


and a column vector would have the dimensions n × 1.

A vector in MATLAB is equivalent to what is called a one-dimensional array


in other languages.
•A matrix can be visualized as a table of values. The dimensions of a
matrix
are r × c, where r is the number of rows and c is the number of
columns. This is pronounced “r by c.”
•A scalar (one value) has the dimensions 1 × 1.

•Therefore, vectors and scalars are actually just subsets of matrices.

•MATLAB is written to work with matrices; the name MATLAB is short


for “matrix laboratory.” For this reason, it is very easy to create vector
and matrix variables, and there are many operations and functions that
can be used on vectors and matrices.
•A matrix is equivalent to a two-dimensional array.
Creating Row Vectors

•several ways to create row vector variables.


The Colon Operator

the brackets [ ] are not necessary to define the vector


An other form (first:step:last).
Linspace function
Similarly, the linspace function creates a linearly spaced vector; linspace(x,y,n)
creates a vector with n values in the inclusive range from x to y. For example, the
following creates a vector with five values linearly spaced between 3 and 15,
including the 3 and 15:

Concatenating the vectors.


Referring to and Modifying Elements

A particular element in a vector is accessed using the name of the


vector variable and the element number (or index, or subscript)
in parentheses. In MATLAB, the indices start at 1.

The vector [1 5 10] is called an index vector;


Changing an element in a vector
The value stored in a vector element can be changed by specifying the
index or subscript.

Extending a vector
By using an index, a vector can also be extended.

The gaps in a vector


If there is a gap between the end of the vector and the specified
element, 0’s are filled in.
Creating Column Vectors
One way to create a column vector is by explicitly putting the values in
square brackets, separated by semicolons:

Transpose of a vector
Creating Matrix Variables
Creating a matrix variable is really just a generalization of creating row
and column vector variables. That is, the values within a row are
separated by either spaces or commas, and the different rows are
separated by semicolons.

There must always be the same number of values in each row. If you attempt to
create a matrix in which there are different numbers of values in the rows, the
result will be an error message;
Iterators can also be used for the values on the rows using the colon
operator;

Different rows in the matrix can also be specified by pressing the Enter
key after each row instead of typing a semicolon when entering the
matrix values;
Referring to and Modifying Matrix Elements

To refer to matrix elements, the row and then the column indices are
given in parentheses (always the row index first and then the
column).

It is also possible to refer to a subset of a matrix. For example, this


refers to the first and second rows, second and third columns:
Using a colon for the row index means all rows, regardless of how many,
and using a colon for the column index means all columns.
Dimensions
The length and size functions in MATLAB are used to find array
dimensions.

The length function returns the number of elements in a vector.


The size function returns the number of rows and columns in a
matrix.

For a matrix, the length function will return either the number of
rows or the number of columns, whichever is largest.

MATLAB also has a function, numel, which returns the total number of
elements in any array (vector or matrix). For vectors, this is equivalent
to the length of the vector. For matrices, it is the
product of the number of rows and columns.
using end for the row index would refer to the last row.
Using end for the column index would refer to the last column
Changing Dimensions
In addition to the transpose operator, MATLAB has several built-in
functions that change the dimensions or configuration of matrices,
including reshape, fliplr, flipud, and rot90.

The reshape function changes the dimensions of a matrix.

The fliplr function “flips” the matrix from left to right (in other
words the
left-most column, the first column, becomes the last column and so
forth),
and the flipud functions flips up to down.
The rot90 function rotates the matrix counterclockwise 90
degrees, so for example the value in the top-right corner becomes
instead the top-left corner and the last column becomes the first
row

The function repmat can also be used to create a matrix;


repmat(mat,m,n) creates a larger matrix, which consists of an m ×
n matrix of copies of mat.
Empty Vectors
An empty vector, or, in other words, a vector that stores
no values, can be created using empty square brackets

Empty vectors can also be used to delete elements from arrays.


MAT LAB Scripts
A computer program is a sequence of instructions, in a given
language, that accomplishes a task. To execute, or run, a program is to
have the computer actually follow these instructions.

High-level languages have English-like commands and functions, such


as “print this” or “if x < 5 do something.” The computer, however, can
interpret commands only written in its machine language.
Programs that are written in high level languages must therefore be
translated into machine language before the computer can actually
execute the sequence of instructions in the program.
A program that does this translation from a high-level language to an
executable file is called a compiler. The original program is called the
source code, and the resulting executable program is called the object
code.
By contrast, an interpreter goes through the code line-by-line, executing
each command as it goes. MATLAB uses either what are called script files
or M-files (the reason for this is that the extension on the filename is .m).
These script files are interpreted, rather than compiled. Therefore, the
correct terminology is that these are scripts, and not programs.

A script is a sequence of MATLAB instructions that is stored in a file and


saved. The contents of a script can be displayed in the Command Window
using the type command. The script can be executed, or run, by simply
entering the name of the file (without the .m extension).

Lets have a look at script file editor


Comments for documentation
It is very important that all scripts be documented well, so that
people can understand what the script does and how it
accomplishes that. One way of documenting a script is to put
comments in it.

In MATLAB, a comment is anything from a % to the end of that


particular line. Comments are completely ignored when the script
is executed.
Input Function
Input statements read in values from the default or standard input
device. In most systems, the default input device is the keyboard, so
the input statement reads in values that have been entered by the
user, or the person who is running the script. In order to let the user
know what he or she is supposed to enter, the script must first prompt
the user for the specified values.
The input function is used in an assignment statement. To call it, a
string is passed, which is the prompt that will appear on the screen,
and whatever the user types will be stored in the variable named on
the left of the assignment statement. To make it easier to read the
prompt, put a colon and then a space after the prompt.

If character or string input is desired, ‘s’ must be added after the prompt:

You might also like