Basic Concepts in MATLAB: 1.1 Performance Objectives
Basic Concepts in MATLAB: 1.1 Performance Objectives
1.3.1 The MATLAB Environment specialized topics like fuzzy logic, neural
networks, signal processing, etc. It can be
used in two different ways: as a traditional
programming environment and as an
After launching MATLAB (Version 7,
interactive calculator. In calculator mode,
Release 14), a multipanel window appears
the built-in and toolbox function provide a
containing Command Window,
convenient means of performing one-off
Workspace, Current Directory, and
calculations and graphical plotting; in
Command History panels, among others.
programming mode, it provides a
This, along with windows for the
programming environment (editor,
Editor/Debugger, Array Editor, and Help
debugger, and profiler) that enables the
Browser that can be invoked as needed, is
user to write their own functions and
the MATLAB environment.
scripts.
n =
Information about any function or toolbox
is available via the command-line help 1
function (or from the Help Browser
launched from the Help menu):
>> a=[1 2 3]
a =
>> help sum
1 2 3
In MATLAB, everything that can be done
using the GUI interface can also be
accomplished using a command-line
>> M = [1 2 3; 3 2 1]
equivalent. The command-line equivalent
is useful because it can be placed into M =
scripts that can be executed automatically.
1 2 3
3 2 1
1.3.2. Creating Arrays
M= 1 2 3
Matrix
[
3 2 1 ] is a 2 ×
M = [1 2 3; 3 2 1];
3 array.
An empty array is considered a 0 × 0 1
matrix:
>> a=zeros(1,5)
>> a = [] a =
a = 0 0 0 0 0
[]
>> a=rand(1,3)
>> a = 1:5 a =
a = 0.9501 0.2311 0.6068
1 2 3 4 5 >> b=rand(1,3)
b =
>> a = 10:-2:1 0.4860 0.8913 0.7621
a =
10 8 6 4 2
A random permutation of the integers 1 to
n can be generated using the
randperm(n) function:
>> a=ones(5,1)
a =
>> randperm(5)
1
1
ans =
1
2 4 1 5 3
1
The variables currently in the workspace
are visible in the Workspace panel, or
By default the rand command generates through the whos command:
number between interval 0 and 1. To
create a random number between interval
a and b, we can use following command.
>> whos
For example,
>> eye(3)
ans =
To save arrays M and a to a file:
1 0 0
0 1 0
>> save myvar M a
0 0 1
1 2 3
>> whos 3 2 1
1 2 3
ans =
>> M 1
M =
3 2 1
ans =
1 3 3
3 1 1
The vector [1 3] is an index array, where To select the elements along the main
each element corresponds to a column diagonal:
index number of the original matrix M.
>> diag(B)
The keyword end can be used to indicate ans =
the last row or column:
3 0
0 1
>> M(:,end)
ans =
If let’s suppose C is an square matrix
3
C= 1 2 ,
1 [ ]
3 4 then
>> M(:,end-1)
>> C = [1 2;3 4]
C =
ans =
1 2
2
3 4
2
>> diag(C)
The selected portion of the one array can
be assigned to a new array: ans =
4
>> B=M(:,3,end)
B =
1.3.5. Changing and Deleting Array >> a([1 3])=[7 8]
Elements
a =
7 6 8 4 5
In calculator mode, the easiest way to
change the elements of an array is to
double click on the array’s name in the >> M(1,2)=100
Workspace panel to launch the Array M =
Editor, which allows manual editing of the
array. (The editor can also be used to 1 100 3
create an array by first generating an array
3 2 1
of zeros at the command line and using the
editor to fill in the nonzero values of the
array.)
To delete selected elements of a vector:
>> a=1:5
>> a([1 4])=[]
a =
a =
1 2 3 4 5
6 4
>> a(2)=6
1 2 3
1.3.6. Manipulating Arrays
3 2 1 1
>> M = [1 2 3; 3 2 1]
M =
>> [M [10 20]'; 30:10:60]
1 2 3
ans =
3 2 1
1 2 3 10
3 2 1 20
>> M' (Transpose)
30 40 50 60
ans =
1 3
>>M(:) (Convert matrix to column vector)
2 2
ans =
3 1
1
3
>> fliplr(M) (flip left/right)
2
ans =
2
3 2 1
3
1 2 3
1
1 2 3
3 2 1 Element-by-element multiplication
Mm×n . *Bm×n = Cm×n
18 8 2
>> 2 + M
>> B = 2*M
>> C = M * B'
B =
C =
2 4 6
28 20
6 4 2
20 28
Matrices are added together in MATLAB
element by element; thus, each matrix
>> C = M' * B
must be the same size; i.e.,
C =
Addition: Mm×n + Bm×n = Cm×n
20 16 12
16 16 16
>> C = M + B
12 16 20
C =
3 6 9
>> a = 1:3
9 6 3
a =
1 2 3
To add vector a to each row of M, a can be
converted into a matrix with the same
>> a * a' dimensions as M. This can be
accomplished in several different ways:
ans =
14
>>ones(2,1)*a (Matrix multiplication)
ans = 1 2 3
14 1 2 3
10
>> ones(2,1) * a + M
ans =
Division (./) and power (.^) operators can
also be preformed element by element. 2 4 6
4 4 4
Addition
>> a(ones(1,2),:) (Tony’s trick)
ans = 15
1 2 3
ans =
1 2 3
Summation 3 2 1
4 4 4
>> a = 1:5
a =
>> sum(M,2) (sum along rows)
1 2 3 4 5
ans =
6
>> sum(a) (Array summation)
6
ans =
>> sum(M,1) (sum along columns) 1 3 4
ans =
1.3.8. Functions and Scripts
4 4 4
>> sum(sum(M)) (sum entire matrix) Functions and scripts in MATLAB are just
text files with a .m extension. User-defined
ans = functions can be used to extend the
capabilities of MATLAB beyond its basic
12
functions. A user-defined function is
treated just like any other function. Scripts
are sequences of expressions that are
Forcing column summation: Even if the automatically executed instead of being
default column summation is desired, it is manually executed one at a time at the
useful (in programming mode) to explicitly command-line. A script uses variables in
specify this in case the matrix has only a the (base) workspace, while each function
single row, in which case it would be has its own (local) workspace for its
treated as a vector and sum to a single variables isolated from other workspaces.
scalar value (an error): Functions communicate only through their
input and output arguments. A function is
distinguished from a script by placing the
keyword function as the first term of the
>> M=M(1,:)(Convert M to singlerow
matrix)
first line in the file.
M =
ans =
The best way to start is to create some
example data for which you know the
correct answer:
>> x = [3 1];
>> P = [1 1; 6 1; 6 5]
P =
1 1
6 1
>> ones(3,1) * x
Given a 2-D point x and m other 2-D points ans =
in P, create a function mydist.m to
determine the Euclidean (i.e., straight-line) 3 1
distance d from x to each of the m points
3 1
in P:
3 1
p 1,1 p1,2
x=[ x 1 x 2 ], P= ⋮
[ ] ⋮
pm , 1 p m ,2
>> ones(3,1)*x - P
ans =
2 0
√( x − p )2 + ( x2 − p1,2 ) 2
[ ]
1 1,1 -3 0
d= ⋮ -3 -4
2 2
√( x − p
1 m,1 ) + ( x 2 − pm , 2 )
Square each element of the result: The M-File Editor can be used to create the
text file for the function mydist. Either
select New, M-File from the File menu, or
>>(ones(3,1)*x - P) .^ 2
ans =
>> edit mydist
4 0
9 0
9 16
ans =
25
2
function d = mydist(x,P)
3
d=sqrt(sum((ones(3,1)*x-P).^2,2));
5
Save the file, then check to see if MATLAB The only thing “hard-coded” in the
can find the file by using the type function is m. The size function can be
command to print the function at the used inside the function to determine the
command line, and then check to see that number of rows (dimension 1) or columns
it works as desired: (dimension 2) in P:
m =
function d = mydist(x,P) 3
d=sqrt(sum((ones(3,1)*x-P).^2,2));
d = n =
2 2
2.8284
MYDIST Euclidean distance from x
4.2426 to P.
7.0711
d = mydist(x,P)
x= n-element vector single point >> a > 0 (Greater than)
totalDistance = sum(mydist(x,P))
>> a ~= 0 (Not equal to)
totalDistance =
ans =
10
1 0 1 1 0
>> a = [4 0 -2 7 0]
>> ~((a < 0)|(a > 4)) (Logical
a = NOT)
4 0 -2 7 0 ans =
1 1 0 0 1
Unlike a regular matrix, a cell array can be
used to store rows of different lengths:
A logical array can be used just like an
index array to select
and change the elements of an array; e.g., >> c = {[10 20 30],[40],[50 60]}
c =
ans =
a =
>> c{1}
4 0 -2 8 0
ans =
1 2 3
>> a(a ~= 0) = a(a ~= 0) + 1
a =
To access the elements within each row:
5 0 -1 9 0
10
Cell Arrays
A cell array is a generalization of the basic To add an element to the end of the first
array in MATLAB that can have as its row:
elements any type of MATLAB data
structure. Curly braces, { }, are used to
distinguish a cell array from a basic array.
>> c{1}(end+1) = 35
c =
[1x4 double] [40] [1x2 double] Some functions can accept a cell array as
input:
>> s = sort(s)
>> c{1}
s =
ans =
'Boston' 'Detroit' 'Miami'
10 20 30 35
c = >> xP = {x, P}
>> d = mydist(xP{:})
A common use for cell arrays is to store
d =
text strings:
2
3
>>s={'Miami','Detroit','Boston'}
5
s =
c =
[] [] [] >> s.Age = 44
s =
Name: 'Mike'
Non-empty values can then be assigned to
each element using a FOR Loop (see Age: 44
Section 11 below).
s =
>> [c2{1:3}] = deal(0)
1x2 struct array with fields:
c2 =
Name
[0] [0] [0]
Age
>> [c3{1:3}] = deal(1,2,3)
c3 =
>> s(2).Age = 40;
[1] [2] [3]
s(2)
ans =
Structures
Name: 'Bill'
Structures are used to group together
related information. Age: 40
s.Name
Each element of a structure is a field:
ans =
Mike
>> s.Name = 'Mike'
ans = D3 =
Bill 1 2 3
4 5 6
1 2 3
>> t.Name = {'Mike','Bill'}
4 5 6
t =
7 8 9
>> t.Age = [44 40]
10 11 12
t =
4
>> D3 = [1 2 3; 4 5 6]
i =
>> ans(:,:,2) = 1
7 i =
10 2
>> D2 = squeeze(D(:,1,:))
1 7
4 10 i =
5
1.3.11. Control Structures i =
chararray =
i abc
b
>> for i = 1:length(c),
i =
c{i} = c{i} + 1;
c
end
11 21 31
Most of the standard arithmetic
ans =
operations and functions in Matlab cannot
be directly applied to an entire cell array; 41
instead, a FOR loop can used to apply the
ans =
operation or function to each element of
the cell array: 51 61
>> if n > 0
>> c = c + 1 disp('Positive value.')
elseif n < 0
else n =
disp('Zero.') 1
end n =
WHILE Loop n =
n =
>> a = [5 0 -1 9 0];
0
>> a > 0
1 0 0 1 0
>> done = 0;
ans =
if n >= 3, done = 1; end 1
>> all(a > 0)
ans = 0
Basic Concepts in MATLAB
______________________________________________________________________
______________________________________________________________________
A= 1 2 3 4 B= 11 22 33 44
2. Let’s
[
5 6 7 8 ] , and
[
55 66 77 88 ] , Combine these two matrices in
such a way that the resultant should be
RES= 55 66 77 88 4 3 2 1
[
11 22 33 44 8 7 6 5 .
]
______________________________________________________________________
______________________________________________________________________
3. Create your own function named as triarea; find the sum of two triangle areas using that
function. The values of all the sides’ measurements should be entered at run time.
______________________________________________________________________
______________________________________________________________________
4. Which of the numbers in A = [1 4 5 6 8 10 12 15 16 18 20] are divisible by 5, How would you
determine through MATLAB functions. Write sequence of instructions.
______________________________________________________________________
______________________________________________________________________
5. 10 students are to be assigned different grades like A, B & C. Save them and after saving,
arrange them through MATLAB function according to their grades i.e. A grade acquiring students
should be placed first then B grade holders, then C.
______________________________________________________________________
______________________________________________________________________
6. Write a program using else if structure to display even and odd numbers from 1 to 20.
______________________________________________________________________
______________________________________________________________________
Due acknowledgments to authors of Lab Book on Neural Network & Fuzzy Logic with MATLAB