This document provides an overview of variables, arrays, and other basic programming concepts in MATLAB. It discusses how variables store and retrieve values, how arrays can have multiple dimensions and elements can be accessed using indexing, and how basic operations can be performed on arrays element-wise or across entire arrays using functions. Various functions for creating arrays filled with zeros, ones, or random values are also introduced.
A basic overview, application and usage of MATLAB for engineers. It covered very basics essential that will help one to get started with MATLAB programming easily.
Provided by IDEAS2IGNITE
A basic overview, application and usage of MATLAB for engineers. It covered very basics essential that will help one to get started with MATLAB programming easily.
Provided by IDEAS2IGNITE
In MATLAB, a vector is created by assigning the elements of the vector to a variable. This can be done in several ways depending on the source of the information.
—Enter an explicit list of elements
—Load matrices from external data files
—Using built-in functions
—Using own functions in M-files
A Powerpoint Presentation designed to provide beginners to MATLAB an introduction to the MATLAB environment and introduce them to the fundamentals of MATLAB including matrix generation and manipulation, Arrays, MATLAB Graphics, Data Import and Export, etc
BIRCH (balanced iterative reducing and clustering using hierarchies) is an unsupervised data-mining algorithm used to perform hierarchical clustering over, particularly large data sets.
In MATLAB, a vector is created by assigning the elements of the vector to a variable. This can be done in several ways depending on the source of the information.
—Enter an explicit list of elements
—Load matrices from external data files
—Using built-in functions
—Using own functions in M-files
A Powerpoint Presentation designed to provide beginners to MATLAB an introduction to the MATLAB environment and introduce them to the fundamentals of MATLAB including matrix generation and manipulation, Arrays, MATLAB Graphics, Data Import and Export, etc
BIRCH (balanced iterative reducing and clustering using hierarchies) is an unsupervised data-mining algorithm used to perform hierarchical clustering over, particularly large data sets.
Learn to manipulate numbers in R using the built in numeric functions. This tutorial is part of the Working With Data module of the R Programming course offered by r-squared.
metode numerik stepest descent dengan rerata aritmatika dipresentasikan pada seminar nasional matematika universitas negeri malang pada tanggal 13 agustus 2016
MATLAB Script or programs are sequences of MATLAB commands saved in plain text files. When you type the name of the script file at the MATLAB prompt the commands in the script file are executed as if you had typed them in command window. Code for a script is done in an Editor window and saved as m-file.
In case your code has errors, MATLAB will show an error message in the command window, when you try to run the program .
Error message will be hyperlinked to the line in the file that caused the error.
User-defined functions are similar to the MATLAB pre-defined functions. A function is a MATLAB program that can accept inputs and produce outputs. A function can be called or executed by another program or function.
Code for a function is done in an Editor window or any text editor same way as script and saved as m-file. The m-file must have the same name as the function.
A MATLAB function that accepts another function as an input is called a function function. Function handles are used for passing functions to function functions. Syntax for function function is same as simple functions, but one or more input arguments will be function handles.
Multiple functions within one function file is called local function. Name of function file should be name of main function. Main function can be called from the command window or any other function. Local functions are typed in any order after the main function. Local functions are only visible to other functions in the same file.
A private function is a function residing in a sub directory with the name private. Private functions are visible only to functions in the parent directory.
The name MATLAB stands for MATrix LABoratory.MATLAB is a high-performance language for technical computing.
It integrates computation, visualization, and programming environment. Furthermore, MATLAB is a modern programming language environment: it has sophisticated data structures, contains built-in editing and debugging tools, and supports object-oriented programming.
These factor make MATLAB an excellent tool for teaching and research.
A short term course on Matlab for Beginners. It gives an insight of the basic commands used for Matlab programming. This course is useful for them who have no prior knowledge in Matlab programming and want to their projects bases on Matlab.
1. Introduction to MATLAB and programming
2. Workspace, variables and arrays
3. Using operators, expressions and statements
4. Repeating and decision-making
5. Different methods for input and output
6. Common functions
7. Logical vectors
8. Matrices and string arrays
9. Introduction to graphics
10. Loops
11. Custom functions and M-files
2. Variables allow you to store (intermediate) results
• a variable is a named location in computer memory
• for storing/retrieving one or more values
• created in MATLAB by assignment
radius = 3
• accessed by mentioning the name (or in Workspace)
>>radius
radius =
3
• can be used anywhere a number (literal) can be used
area = pi*radius^2
CIV1900 Engineering Skills: programming in MATLAB 2
3. Variables in MATLAB
• variables are listed in alphabetical order in the Workspace
• with information about their name, size, type and min/max
• not all information is shown automatically
• use View > Choose Columns when focus is in the Workspace
• MATLAB automatically creates a variable called ans if
needed:
>> 1024^3/8
ans=
134217728
• If you don't want to print out the result add a semi-colon
>> diameter = 2*radius;
3Engineering Skills: programming in MATLABCIV1900
4. Assignment might look like algebra, but it isn't
• x = x + 1 doesn't sound right
• how can x be equal to x + 1
• why isn't it a logical inconsistency?
• because assignment isn't equality at all
• assignment is a two step process:
• calculate the value on the right hand side (r-value)
• store the result in the variable on the left hand side (l-value)
• So x = x + 1 means:
• evaluate x + 1 first by getting the value out of variable x
• store the result back into variable x
4Engineering Skills: programming in MATLABCIV1900
5. MATLAB arrays are collections of (like) values
• arrays store multiple elements of one type
• each element can be accessed by position in the array
• called indexing or subscripting the array
• uses the array name and then the index in
parentheses
• most other programming languages index from 0
5Engineering Skills: programming in MATLABCIV1900
6. 1-dimensional arrays are called vectors
• created with square brackets and (optional) commas
pos = [1, 0, -1]
primes = [1 2 3 5 7 11 13]
• accessed with indices e.g. the 6th prime number is?
primes(6)
ans =
11
• notice how vectors appear in the Workspace (e.g. size 1x7)
6Engineering Skills: programming in MATLABCIV1900
7. Vectors can be created using n:s:m notation
• vectors ranging from n to m with step s can be written n:s:m
e.g. from 1 to 20 stepping by 3:
x = 1:3:20
x =
1 4 7 10 13 16 19
• if the step size is missing, the default is 1:
x = 1:7
x =
1 2 3 4 5 6 7
• the vector goes up to and including the last value
• think about what might happen with negative values!
7Engineering Skills: programming in MATLABCIV1900
8. MATLAB has functions to create vectors with fixed sizes
• linspace takes a n and m, and a number of elements:
• e.g. create a vector from 0 to 3 containing 5 values
linspace(0, 3, 5)
ans =
0 0.7500 1.5000 2.2500 3.0000
• zeros and ones create vectors of only zeros and ones
zeros(1, 5)
ans =
0 0 0 0 0
ones(1, 5)
ans =
1 1 1 1 1
8Engineering Skills: programming in MATLABCIV1900
9. Indexing is used to make vectors longer or shorter
• assigning to an index beyond the length grows the vector
data = [1 2 3];
data(6) = -1
data =
1 2 3 0 0 -1
• zeros are used to fill in the gaps
• assigning an empty vector to an index removes elements
data(2) = []
data =
1 3 0 0 -1
9Engineering Skills: programming in MATLABCIV1900
10. Boolean vectors can be used to select elements
• booleans are true/false (that is, yes/no values) of type logical
primes = [1 2 3 5 7 11 13];
mask = [true false true false]
mask =
1 0 1 0
primes(mask)
ans =
1 3
• the new vector is the length of the number of true values
• booleans may look like numbers when printed
but they are a different type
10Engineering Skills: programming in MATLABCIV1900
11. Index vectors can select elements in any order
• each element in the index vector selects an element
primes = [1 2 3 5 7 11 13];
indices = [1 6 4];
primes(indices)
ans =
1 11 5
• the index vector can be of any length
• the new vector has the same length as the index vector
• the index vector can be created using n:s:m range notation
• the special value end can be used in these ranges
11Engineering Skills: programming in MATLABCIV1900
12. Arrays can store many dimensions
• matrices are two dimensional arrays
• created with semi-colons to separate the rows:
x = [1 2 3; 4 5 6; 7 8 9]
x =
1 2 3
4 5 6
7 8 9
• accessed using a pair of indices (row first, then column)
x(1, 3)
ans =
3
• functions like zeros and ones work too
12Engineering Skills: programming in MATLABCIV1900
13. Functions apply to arrays in different ways
• some functions apply to all elements of an array
• e.g. min, max, sum, …
values = [0 5 -2];
sum(values)
ans =
3
• others apply to each element one at a time
• e.g. trig functions, absolute value (abs), …
abs(values)
ans =
0 5 2
13Engineering Skills: programming in MATLABCIV1900
14. Special operators exist for per element calculations
• the regular operators sometimes behave differently on arrays
• e.g. * does not multiply corresponding array elements
[1 2 3]*[4 5 6]
gives the error: Inner matrix dimensions must agree
• because * is matrix multiply (more about this in later weeks)
• we need array multiply which multiplies each pair of elements
to create a new array:
[1 2 3].*[4 5 6]
ans =
4 10 18
14Engineering Skills: programming in MATLABCIV1900
15. Concatenating and slicing matrices
15
>> a=[1 2 3; 5 7 9; 8 9 10]
>> b=[9 8 7; 6 5 4; 1 2 3]
• What would be the result?
>> c=[a b]
c = 1 2 3 9 8 7
5 7 9 6 5 4
8 9 10 1 2 3
>> d=[a; b]
d = 1 2 3
5 7 9
8 9 10
9 8 7
6 5 4
1 2 3
>> e=a(1,:)
e = 1 2 3
“1” means “the first row”
“:” means “all columns”
>> f=a(:,1)
f = 1
5
8
“:” means “all rows”
“1” means “the first column”
Engineering Skills: programming in MATLABCIV1900
16. Deleting rows and columns
16
• Easy, by using []
>>c
c = 1 2 3 9 8 7
5 7 9 6 5 4
8 9 10 1 2 3
• To delete the second column:
>> c(:,2)=[]
c = 1 3 9 8 7
5 9 6 5 4
8 10 1 2 3
• To further delete the second row:
>> c(2,:)=[]
c = 1 3 9 8 7
8 10 1 2 3
Engineering Skills: programming in MATLABCIV1900
17. 17
Transpose of a matrix
17
• If a is a m x n matrix, then the transpose of a, denoted with a’,
is a n x m matrix whose first column is the first row of a, whose
second column is the second row of a, and so on
• In Matlab we can compute the transpose of a matrix using the
dot-apostrophe operator „
>>a=[1 2 3 4; 5 7 9 3; 8 9 10 12]
a = 1 2 3 4
5 7 9 3
8 9 10 12
>> a'
ans = 1 5 8
2 7 9
3 9 10
4 3 12
3 x 4
4 x 3
Engineering Skills: programming in MATLABCIV1900
18. 18
Generating basic matrices
18
• zeros() – all elements are 0
>> zeros(2,3)
ans = 0 0 0
0 0 0
• ones() – all elements are 1
>> ones(2,3)
ans = 1 1 1
1 1 1
• rand() – uniformly distributed
random elements from (0,1)
>> rand(2,3)
ans = 0.8147 0.1270 0.6324
0.9058 0.9134 0.0975
• eye() – identity matrix
>> eye(3)
>>ans =
1 0 0
0 1 0
0 0 1
>> eye(5)
ans =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
CIV1900 Engineering Skills: programming in MATLAB
19. 19
• Done element by element
• Matrices must have the same dimensions
>> a=[1,2,3; 5,7,9; 8,9,10]
a = 1 2 3
5 7 9
8 9 10
>> b=[9,8,7; 6,5,4; 1,2,3]
b = 9 8 7
6 5 4
1 2 3
• What would be the result?
>> c=a+b
c = 10 10 10
11 12 13
9 11 13
Adding and subtracting matrices
>> d=a-b
d = -8 -6 -4
-1 2 5
7 7 7
Engineering Skills: programming in MATLABCIV1900
20. 20
• Each matrix element is multiplied by the scalar
>> a=[1,2,3; 5,7,9; 8,9,10]
a = 1 2 3
5 7 9
8 9 10
>> b=a*6
b = 6 12 18
30 42 54
48 54 60
• Do we need to use .* instead of * ?
Multiplying a matrix with a scalar
Engineering Skills: programming in MATLABCIV1900
21. Calculating the inverse matrix
• Let a, b and c are square matrices, a*b=c and
we are given a and c and need to find b
>> a=[9 2 7; 6 1 4; 1 6 3]
>> c=[1 2 3; 5 7 9; 8 9 10]
• Let‟s do it analytically:
21
• In Matlab we can use inv():
>> b=inv(a)*c
b = 5.9643 7.8214 9.6786
4.7857 5.9286 7.0714
-8.8929 -11.4643 -14.0357
cab
caaba
cab
1
1 1
multiply both sides on theleft by 1
a
, where I is the identity matrixIaa
1
Engineering Skills: programming in MATLABCIV1900
22. Matlab can manipulate not only numbers but also strings
• A character string or simply string is an ordered sequence of
characters (i.e. symbols and digits)
• In Matlab strings are enclosed in single quotes
>> s1 = 'Hello!'
s1 = Hello!
>> s2 = 'I am 20 years old.'
s2 = I am 20 years old.
• Single quotes can be included in the strings with double quotes
>> s4 = 'You''re smart'
s4 = You're smart
22Engineering Skills: programming in MATLABCIV1900
23. Matlab treats strings as arrays of characters
• We can apply the vector manipulation functions
• What is the result?
>> s1 = 'James';
>> size(s1)
ans = 1 5
>> length(s1)
ans = 5
>> s1(3)
ans = m
>> s1(6)
??? Attempted to access s1(6); index out of bounds
because numel(s1)=5.
23Engineering Skills: programming in MATLABCIV1900
24. Displaying string variables with disp()
• We already have seen how to use disp()
• num2str() must be used to convert numbers intro strings,
which are then concatenated with other strings in disp()
>> disp(6)
6
>> disp(['My favourite number is ', a])
My favourite number is
>> disp(['My favourite number is ', int2str(a)])
My favourite number is 6
24Engineering Skills: programming in MATLABCIV1900
25. Displaying string variables with fprintf()
• There are other display and print functions which do not require
numbers to be converted to strings to display information, e.g.
fprintf()
>> fprintf('My favourite number is %d n', a);
My favourite number is 6
%d – print the value of the variable a as an integer
n – the cursor goes to a new line
Other useful formatting symbols:
%f – float point number
%s – string
t – insert tab
2525Engineering Skills: programming in MATLABCIV1900