Matlab
Matlab
Row 1
Row 2
Row 3
arr(3,2)
Row 4
1
c= 3 3x1 array 3 elements, column vector
5
Row # Column #
• length(arr)
• size(arr)
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.
Roshan Tishraj Patroo 15
MATLAB FOR DUMMIES
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
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
• 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
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
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