MATLAB Basics: CS 111 Introduction To Computing in Engineering and Science
MATLAB Basics: CS 111 Introduction To Computing in Engineering and Science
MATLAB Basics: CS 111 Introduction To Computing in Engineering and Science
Row 1
Row 2
Row 3
arr(3,2)
Row 4
Arrays
• The fundamental unit of data in MATLAB
• Scalars are also treated as arrays by MATLAB (1
row and 1 column).
• Row and column indices of an array start from 1.
• Arrays can be classified as vectors and
matrices.
CS 111 3
MATLAB BASICS
CS 111 4
MATLAB BASICS
1 2
a= 3 4 3x2 matrix 6 elements
5 6
1
c= 3 3x1 array 3 elements, column vector
5
Row # Column #
CS 111 5
MATLAB BASICS
Variables
• A region of memory containing an array, which is known
by a user-specified name.
• Contents can be used or modified at any time.
• Variable names must begin with a letter, followed by any
combination of letters, numbers and the underscore (_)
character. Only the first 31 characters are significant.
• The MATLAB language is Case Sensitive. NAME, name
and Name are all different variables.
Give meaningful (descriptive and easy-to-remember)
names for the variables. Never define a variable with the
same name as a MATLAB function or command.
CS 111 6
MATLAB BASICS
CS 111 9
MATLAB BASICS
• length(arr)
• size(arr)
CS 111 11
MATLAB BASICS
CS 111 12
MATLAB BASICS
Multidimensional Arrays
• A two dimensional array with m rows and n columns
will occupy mxn successive locations in the computer’s
memory. MATLAB always allocates array elements in
column major order.
1
a= [1 2 3; 4 5 6; 7 8 9; 10 11 12];
4
a(5) = a(1,2) = 2 1 2 3 7
Subarrays
• The end function: When used in an array subscript, it
returns the highest value taken on by that subscript.
arr3 = [1 2 3 4 5 6 7 8];
arr3(5:end) is the array [5 6 7 8]
arr4 = [1 2 3 4; 5 6 7 8; 9 10 11 12];
arr4(2:end, 2:end)
• Using subarrays on the left hand-side of an assignment
statement:
arr4(1:2, [1 4]) = [20 21; 22 23];
(1,1) (1,4) (2,1) and (2,4) are updated.
arr4 = [20 21; 22 23]; all of the array is changed.
CS 111 15
MATLAB BASICS
Subarrays
• Assigning a Scalar to a Subarray: A scalar value on the
right-hand side of an assignment statement is copied
into every element specified on the left-hand side.
>> arr4 = [1 2 3 4; 5 6 7 8; 9 10 11 12];
>> arr4(1:2, 1:2) = 1
arr4 =
1 1 3 4
1 1 7 8
9 10 11 12
CS 111 16
MATLAB BASICS
Special Values
• MATLAB includes a number of predefined special values.
These values can be used at any time without initializing
them.
• These predefined values are stored in ordinary variables.
They can be overwritten or modified by a user.
• If a new value is assigned to one of these variables, then
that new value will replace the default one in all later
calculations.
>> circ1 = 2 * pi * 10;
>> pi = 3;
>> circ2 = 2 * pi * 10;
Never change the values of predefined variables.
CS 111 17
MATLAB BASICS
Special Values
• pi: value up to 15 significant digits
• i, j: sqrt(-1)
• Inf: infinity (such as division by 0)
• NaN: Not-a-Number (division of zero by zero)
• clock: current date and time in the form of a 6-element
row vector containing the year, month, day, hour,
minute, and second
• date: current date as a string such as 16-Feb-2004
• eps: epsilon is the smallest difference between two
numbers
• ans: stores the result of an expression
CS 111 18
MATLAB BASICS
CS 111 19
MATLAB BASICS
CS 111 20
MATLAB BASICS
CS 111 21
MATLAB BASICS
CS 111 22
MATLAB BASICS
>> fprintf( 'Result is %d', 3 )
Result is 3
>> fprintf( 'Area of a circle with radius %d is %f', 3, pi*3^2 )
Area of a circle with radius 3 is 28.274334
>> x = 5;
>> fprintf( 'x = %3d', x )
x= 5
>> x = pi;
>> fprintf( 'x = %0.2f', x )
x = 3.14
>> fprintf( 'x = %6.2f', x )
x = 3.14
>> fprintf( 'x = %d\ny = %d\n', 3, 13 )
x=3
y = 13
CS 111 23
MATLAB BASICS
Data files
• save filename var1 var2 …
>> save myfile.mat x y binary
>> save myfile.dat x –ascii ascii
• load filename
>> load myfile.mat binary
>> load myfile.dat –ascii ascii
CS 111 24
MATLAB BASICS
• variable_name = expression;
– addition a+b a+b
– subtraction a-b a-b
– multiplication axb a*b
– division a/b a/b
– exponent ab a^b
CS 111 25
MATLAB BASICS
Hierarchy of operations
• x=3*2+6/2
• Processing order of operations is important
– parentheses (starting from the innermost)
– exponentials (from left to right)
– multiplications and divisions (from left to right)
– additions and subtractions (from left to right)
>> x = 3 * 2 + 6 / 2
x=
9
CS 111 26
MATLAB BASICS
CS 111 27
MATLAB BASICS
CS 111 28
MATLAB BASICS
Summary
• help command Online help
• lookfor keyword Lists related commands
• which Version and location info
• clear Clears the workspace
• clc Clears the command window
• diary filename Sends output to file
• diary on/off Turns diary on/off
• who, whos Lists content of the workspace
• more on/off Enables/disables paged output
• Ctrl+c Aborts operation
• … Continuation
• % Comments
CS 111 29