Matlab Introduction: C. Jairaj Department of Electronics Engineering, KIT's COE, Kolhapur
Matlab Introduction: C. Jairaj Department of Electronics Engineering, KIT's COE, Kolhapur
C. Jairaj
Department of Electronics Engineering,
KIT’s COE, Kolhapur
[email protected]
Editor Window
Workspace
and current
directory
Command
Command Window
History
4/28/2019 KIT'S COEK 5
Getting Started
Row 1
Row 2
Row 3
arr(3,2)
Row 4
1
c= 3 3x1 array 3 elements, column vector
5
Row # Column #
KIT'S COEK
4/28/2019 14
Variables contd…4
Initializing Variables in Assignment Statements
• Arrays are constructed using brackets and
semicolons. All of the elements of an array are listed
in row order.
• The values in each row are listed from left to right
and they are separated by blank spaces or commas.
• The rows are separated by semicolons or new lines.
• The number of elements in every row of an array
must be the same.
• The expressions used to initialize arrays can include
algebraic operations and all or portions of previously
defined arrays.
KIT'S COEK
4/28/2019 15
Variables contd…5
• length(arr)
• size(arr)
4/28/2019 KIT'S COEK 16
Arrays
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
4 5 6 10
• A 2x3x2 array of three dimensions 7 8 9 2
c(:, :, 1) = [1 2 3; 4 5 6 ]; 10 11 12 5
c(:, :, 2) = [7 8 9; 10 11 12]; 8
11
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.
4/28/2019 KIT'S COEK 19
Arrays contd…3
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
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
== Equal to
~= Not equal to