You are on page 1of 7

Introduction

MATLAB – MATrix LABoratory


• Initially developed by a lecturer in 1970’s to help students learn
linear algebra.
• It was later marketed and further developed under MathWorks
Inc. (founded in 1984) – www.mathworks.com
• Matlab is a software package which can be used to perform
analysis and solve mathematical and engineering problems.
• It has excellent programming features and graphics capability –
easy to learn and flexible.
• Available in many operating systems – Windows, Macintosh,
Unix, DOS
• It has several tooboxes to solve specific problems.
Variables

ALL variables are matrices

3
2
 
9  2 1 5 6
4  
3
3 2 1 7 9 3 2 4 
 
1x1 4x1 1x4 2x4

Variables
•They are case sensitive i.e a  A, x  X
•Their names can contain up to 31 characters
•Must start with a letter followed by any combination of letters and numbers

Variables are stored in workspace


Assigns value to a variable

>> a=3
a =
3
>> b=4
b =
4
>> R=a/b
R =
0.7500
>>

>> whos
Name Size Bytes Class
R 1x1 8 double array
a 1x1 8 double array
b 1x1 8 double array
Grand total is 3 elements using 24 bytes
Assign values to vectors

>> A = [1 2 3 4 5] A row vector – values


A =
1 2 3 4 5
are separated by spaces
>> B = [5,6,7,8,9] or commas (,)
B =
5 6 7 8 9

>> c = [10;12;14;16;18]
c =
10
12
A column
14 vector – values
16 are separated by
18 semi–colon (;)
>>
Assign values to matrices

>> A=[1 2 3;4, 5, 6;7 8 9]


A =
1 2 3
1 2 3  4 5 6
4 5 6  
7 8 9 7 8 9
>>

Columns separated by Rows separated by


space or a comma semi-colon
Assign values to matrices
>> a=[1:8]
a =
1 2 3 4 5 6 7 8
>> b=[2:2:12]
b =
2 4 6 8 10 12

>> c=[20:-5:0]
c =
20 15 10 5 0

>> d=[ c ; 20:-2:12 ; 7, 12, 5 1 7 ]


d =
20 15 10 5 0
20 18 16 14 12
7 12 5 1 7
Access elements in a matrix or a vector 1 2 3
A  4 5 6
>> A(2,[1 2 3])
>> b = A(2,3)
ans =
7 8 9
b =
6 4 5 6

>> A(3,3) >> c = A([1 2 3],2)


ans = c =
9 2
5
>> A(:,3) 8
ans =
3 >> d = A([1 3],[2 3])
6 d =
9 2 3
8 9
>> A(1,:)
ans =
>> A([2 3],:)
1 2 3
ans =
4 5 6
7 8 9

You might also like