ELEC 200 - Engineering Graphics - Fall 2004
MatLab - Basic Syntax
>> % this is a comment
>> # this is also a comment
>> u = [1 2 3] % a row vector
u =
1 2 3
>> v = [4; 5; 6] % a column vector
v =
4
5
6
>> u + v % sum of a row vector and a column vector is undefined
error: operator +: nonconformant arguments (op1 is 3x1, op2 is 1x3)
error: evaluating binary operator `+' near line 4, column 2
>>
>> % u and v have the same dimensionality, but different "shapes"
>> % but we can make them fit by transposing one of them, using "'"
>> % the transpose operator flips rows <=> columns
>>
>> u + v' % sum of two row vectors is defined
ans =
5 7 9
>> u' + v % sum of two column vectors is defined
ans =
5
7
9
>> % there are two kinds of vector multiplication, denoted by "*",
>> % depending on the order of the operands when they are different shapes
>>
>> u * v % inner product: (row vector)*(column vector) = (scalar)
ans = 32
>> v * u % outer product: (column vector)*(row vector) = (matrix)
ans =
4 8 12
5 10 15
6 12 18
>> % but if the operands are the same shape, the operation fails
>>
>> u * v' % (row vector)*(row vector)
error: operator *: nonconformant arguments (op1 is 1x3, op2 is 1x3)
error: evaluating binary operator `*' near line 18, column 3
>>
>> u' * v % (column vector)*(column vector)
error: operator *: nonconformant arguments (op1 is 3x1, op2 is 3x1)
error: evaluating binary operator `*' near line 18, column 4
>>
>> % the operaton of element-by-element multiplication is ".*", which
>> % fails in exactly those cases where "*" succeeds, and vice versa
>>
>> u .* v % (row vector).*(column vector)
error: product: nonconformant arguments (op1 is 1x3, op2 is 3x1)
error: evaluating binary operator `.*' near line 3, column 2
>>
>> v .* u % (column vector).*(row vector)
error: product: nonconformant arguments (op1 is 1x3, op2 is 3x1)
error: evaluating binary operator `.*' near line 3, column 2
>>
>> u.*v' % (row vector).*(row vector) = (row vector)
ans =
4 10 18
>> u'.*v % (column vector).*(column vector) = (column vector)
ans =
4
10
18
>> % we write matrices by combining the ways we already have of
>> % writing row vectors and column vectors
>>
>> A = [1 2 3; 4 5 6; 7 8 9] % a column of row vectors is a matrix
A =
1 2 3
4 5 6
7 8 9
>> B = [[1;4;7][2;5;8][3;6;9]]% the same matrix as a row of column vectors
B =
1 2 3
4 5 6
7 8 9
>> % we can also build matrices by combining objects already defined
>> C = [u; v'; 7 8 9]
C =
1 2 3
4 5 6
7 8 9
>> % finally, we can write matrices out explicitly
>> D = [
1 2 3
4 5 6
7 8 9
]
D =
1 2 3
4 5 6
7 8 9
>> % we can refer to elements of vectors and matrices by index
>> % and vectors are just treated as 1xN or Nx1 matrices
>>
>> u(2) % u is a 3-element row vector, or 1x3 matrix
ans = 2
>> u(1,2) % so this works
ans = 2
>> u(2,1) % and this doesn't
error: invalid row index = 2
>>
>> v(2) % v is a 3-element column vector, or 3x1 matrix
ans = 5
>> v(1,2) % so this doesn't work
error: invalid column index = 2
>> v(2,1) % but this does
ans = 5
>>
>> A(1,3) % in general, matrices are indexed by row and column
ans = 3
>> A(3,1)
ans = 7
>> A(3) % but the above indexing syntax leads to this unexpected result
ans = 7
>> A(3,1:3) % to get the entire third row, we write it a bit differently
ans =
7 8 9
>> A(3,:) % which can be abbreviated when referring to the whole row
ans =
7 8 9
>> % many other variations on the theme are possible
>>
>> A(1:2,2:3) % the last two columns of the first two rows
ans =
2 3
5 6
>> A(1:2,:) % the first two rows, in their entirety
ans =
1 2 3
4 5 6
>> A(:,2:3) % the last two columns, in their entirety
ans =
2 3
5 6
8 9
>>