MATLAB Variables
MATLAB Variables
6
MATLAB is as a programming language works on different levels of
variables. The main advantage of MATLAB among other programming
language is its ability to accept and use variables without prior dimensioning or
setting up its characteristics. Unlike in C, C#, Visual Basic where the variables
must be defined before using, MATLAB variables can be used directly as the
value is assigned and can be changed as new value is assigned to the variable.
OBJECTIVES
This chapter aims introduce MATLAB variables and how variables are
indexed. At the end of this lesson, the students should be able to:
1. Identify the different MATLAB Variables.
2. Setup the different format of MATLAB variables
3. Work on with matrices and other functions associated with matrices.
DISCUSSION
The variables of MATLAB when assigned using the assignment operator =
immediately assigns its value to the variable. It does not need to be initialized or
to set up for the quantity that it will hold. Once assigned, it transfers the value
from the left hand of the equation to the variable at the right.
MANAGING THE COMMAND WINDOW/VARIABLE
COMMAND BRIEF DEFINITION
clc
Clears the command window
who, whos
Used to display the variable contents of the workspace.
When a variable name is placed after the who command, it
will display the properties of the said variable
format
Set up the display setup in the display screen. It sets up the
display format based on the attribute placed after the format
command.
MATLAB VARIABLES
3
3
FORMATTING VARIABLES
The attributes available in formatting variables and must be placed after the
format command is as follows:
ATTRIBUTE BRIEF DEFINITION
short
Scaled fixed point format with 5 digits.
long
Scaled fixed point format with 15 digits for double and 7 digits
for single.
short e
Floating point format with 5 digits.
long e
Floating point format with 15 digits for double and 7 digits for
single.
short g
Best of fixed or floating point format with 5 digits.
short eng
Engineering format that has at least 5 digits & a power that is
a multiple of 3
long eng
Engineering format that has exactly 16 significant digits & a
power that is a multiple of 3.
hex
Hexadecimal format.
+
The symbols +, - and blank are printed for positive, negative
and zero elements. Imaginary parts are ignored.
bank
Fixed format for dollars and cents.
rat
Approximation by ratio of small integers.
FORMATTING SCREEN DISPLAY
The following commands are used to format the line spacing of MATLAB
Command Window.
COMMAND BRIEF DEFINITION
format compact
Suppresses extra line-feeds.
format loose
Puts the extra line-feeds back in
MATLAB VARIABLES
The array is the fundamental form that MATLAB uses to store and
manipulate data. An array is a list of numbers arranged in rows and/or columns.
Let us name these MATLAB variables as scalar for a 1-by-1 array; vector for a
row vector with size m-by-1 or a column vector with size 1-by-n; and the
general matrix for any m-by-n array
CREATING MATRICES
Various ways of assigning the elements inside an array can be made by the
following format:
1. Direct Assignment
Syntax: var_name = [type vector elements]
Row vector: Type elements with a space or comma between the elements
inside the square brackets.
Example: >> row = [ 1 2 3,4]
Column vector: Type elements with a semicolon (;) between the elements inside
the square brackets.
Example: >>col = [1;2;3;4]
Matrix: Type the elements of each row separated by space or comma(,),each
row with equal number of elements are separated by semicolon (;).
Example: >>col = [1, 2 3, 4; 5 6 7 8; 9, 10, 11, 12]
2. Colon Operator
The colon operation generates a series of number with equal number of
intervals.
Syntax: var_name = [m:q:n] or var_name = m:q:n
where m is the first term
q is the spacing, absence of this value sets the default q =1
n is the last term
Examples:
>> x = [1:2:13]
>> y = [1.5:-0.1:1]
>> z = [-3:7]
3. Linspace command
The linspace creates a row vector with constant spacing
Syntax: var_name = linspace(m,n,r)
where m is the first term
n is the last term
r is the number of terms.
Example:
>> A = linspace(10,60,6);
>> B = linspace(9,20,12);
>> C = [ linpace(2,5,4); linspace(7,12,4); linspace(9,12,4) ]
ARRAY INDEXING
To access a single element in the matrix,
1. Place inside the parenthesis the index number of the element counting
the elements downward.
2. Place inside the parenthesis, the row number (m), followed by the column
number separated by a comma.
More indexing examples below would be helpful.
ADDRESS LOCATION
A(2) Refers to 2nd element in the vector v
A(3,4) Refers to the element in row 3, column 4
A(:) Represents all the row or column elements of the vector v
A(1:3) Represents the 1st through 3rd elements of v
A(:, 2) Denotes all the elements in the 2nd column of A
A(:, 1:4) Denotes all the elements in the 1st through 4th column of A
A(3, :) Denotes all the elements in the 3rd row of A
A(2:4, :) Denotes all the elements in the 2nd through 4th row of A
A(2:3,1:3) Denotes all the elements in the 2nd through 3rd row that are also in
the 1st
Element wise Assignment
The value inside a matrix can be changed by using matrix indexing.
Example: >> A(2,3) = 10 % Changes the current value of the second row
% third column into 10.
MATRIX ARITHMETIC OPERATIONS
Symbol Operation Symbol Operation
^
Power * Multiplication
/ Right division \ Left Division
+ Addition - Subtraction
Component transpose ./ Component Division
.^ Component Power .* Component Multiplication
Matrix Addition and Subtraction
If A and B are two matrices having the same order, then
the sum of A and B, denoted by A + B, is the matrix for which each of its
elements is the sum of the corresponding elements of A and B.
the difference of A and B, denoted by A B, is defined by A B = A + (-B)
Matrix Multiplication
When performing matrix multiplication, these rules apply:
Inner dimensions must be equal
Dimensions of the resulting matrix are the outermost dimensions of
multiplied matrices
Matrix Division
The division operation is associated with the rules of linear algebra.
MATLAB has two types of array division: the right and the left division.
The left division ( \ ) is used to solve the matrix equation AX = B.
The right division ( / ) is used to solve the equation XC = D.
Scalar Addition
MATLAB allows the addition, subtraction, multiplication and division of
scalars to matrices of any size (known as scalar expansion).
Example: >> w = [1 2;3 4] + 5
Transpose of a Matrix
The transpose of a matrix is a new matrix in which the rows of the original matrix
are the columns of the new matrix. In the command window, just type:
Example: >> B=A
COMMAND BRIEF DEFINITION COMMAND BRIEF DEFINITION
inv( A) Matrix inverse eig( A ) Eigenvalues
det(A ) Determinant svd( A ) Singular value dec
rank( A ) Matrix rank norm( A ) Matrix/vector norm
COMMAND BRIEF DEFINITION
magic(n) Creates an n x n matrix where the sum of its diagonal, rows
and columns are equal
eye(n) Creates an n x n identity matrix
ones(m,n) Creates an m x n matrix of ones
zeros(n) Creates an n x n matrix of zeros
zeros(m,n) Creates an m x n matrix of zeros
size(A) Returns a row vector [m, n] containing the sizes of the m x n
array A
length(A) Computes either the number of elements of A if A is a vector
or the largest value of m or n if A is an m x n matrix
sum(A) Sums the elements in each column of the array A and returns
a row vector containing the sums
sort(A) Sorts each column of the array A in ascending order and
returns an array the same size as A
Concatenation
You can join matrices using square brackets, which is the array
concatenation operator. Within the brackets, a space/comma is used to
separate rows; while a semicolon is used to separate columns.
>> a = [1 2;3 4]
>> cat_a = [ a, 2*a; 3*a, 4*a; 5*a, 6*a]
TOPIC
MATLAB Variables
OBJECTIVES
1. Identify the different MATLAB Variables.
2. Setup the different format of MATLAB variables
3. Work on with matrices and other functions associated with
matrices
NAME
ACCT # TERMINAL#
COURSE /SEC PROG / YEAR
ROOM SCHEDULE
FACULTY
INSTRUCTIONS Answer the following items on the space provided.
1. For the given matrix
(
(
(
(
=
5 4 2 2
9 3 4 4
2 2 6 7
1 1 7 8
A
Use MATLAB Matrix functions to perform the following operations:
Input Matrix A Find the determinant of A
Syntax:
Syntax
Result:
Result:
Inverse of A in fractional form RREF of Matrix A
Syntax:
Syntax
Result:
Result:
A Ac ct ti iv vi it ty y 3 3
For the given matrix B:
(
(
(
(
=
5 4 2 2
9 3 4 4
2 2 6 7
1 1 7 8
B
List down the following elements:
1. B (12) 2. B(3,4) 3. B(2,1)
4. B(2, : ) 5. B(: , 2:3 )
6. B (1:2, 3:end)
Use array indexing in the most simplest way to list down the following elements:
7. 9 8. 8 9. | | 1 1 7 8
10.
(
(
(
(
=
5 2
9 4
2 6
1 7
B
Program Analysis:
The script shown below is a simple m-file script. Narrate on the right side the
processes being done.
PROGRAM NARRATION
% This program finds the inverse
% of a square matrix
disp(Hello, Welcome to Matrix Inverter)
format compact
A = input(Enter Matrix A: [<row1>;<row2>;])
[m,n]=size(A);
augment_A = [A, eye(n)];
rrefA= rref(augment_A);
inverse_A = rrefA(:,(n+1):end);
disp(The inverse of Matrix A);
disp(A);
disp(inverse_A);
MACHINE PROBLEM
Create a simple that would be able to accept Matrices (A and b). A is a square matrix
of order four and b is a column vector. MATLAB would compute for the root of the
equation A x = b using Cramers Rule. The program shall display Aw, Ax, Ay and Az
with their corresponding determinants before showing the value of the roots (w,x,y,z)
PROGRAM COMMENTS