Introduction To Computing With MATLAB: Gemechu Fanta
Introduction To Computing With MATLAB: Gemechu Fanta
Gemechu Fanta
1 / 26
Gemechu Fanta
Matrix Combination
outline of lecture 2
Operations on Matrices
I
I
I
I
I
2 / 26
Gemechu Fanta
Matrix Combination
Gemechu Fanta
Matrix Combination
Gemechu Fanta
Matrix Combination
5 / 26
Gemechu Fanta
Matrix Combination
Data types...
There are a number of data structures which extend the matrix
structure in some what different ways. A prototype of these is a
sparse matrices and cell arrays.
In MATLAB one can store and operate on a matrix of any finite
sizehowever, the memory size and computational performance of
the computer is a factor to dictate the maximal size.
In applications, there are real world problems represented by a
matrix of sufficiently large size but with a few nonzero
components, sparse matrix.
Such matrices arise, for instance, in the analysis of
communication networks, in finite element modeling etc.
One advantage of working with sparse matrices/arrays is, less
storagspace and much shorter evaluation time as compared to
the corresponding full matrix.
6 / 26
Gemechu Fanta
Matrix Combination
Data types...
Example In finite element modeling of
air flow over a wing of an airplane,
computational grids are set up in such
a way that they are densely space
wherever the gradient of the solution is
high. MATLAB has a built-in function,
airfoil that solves this model. Help
facility returns the next result.
>> help airfoil
AIRFOIL Display sparse matrix from
NASA airfoil
>> load airfoil
>> figure
>> plot(x,y,o)
7 / 26 >> axis equal
Gemechu Fanta
Introduction to Computing with MATLAB
Matrix Combination
Data types...
Example In finite element modeling of
air flow over a wing of an airplane,
computational grids are set up in such
a way that they are densely space
wherever the gradient of the solution is
high. MATLAB has a built-in function,
airfoil that solves this model. Help
facility returns the next result.
>> help airfoil
AIRFOIL Display sparse matrix from
NASA airfoil
>> load airfoil
>> figure
>> plot(x,y,o)
7 / 26 >> axis equal
Gemechu Fanta
Introduction to Computing with MATLAB
Matrix Combination
3.2 Strings
A string is an array of characters like, math 621, hello world,
2009 etc, surrounded by quotes. The MATLAB system
represents characters with their ASCII values.
>> x=math 621
x=
math 621
Conversion between a character and the corresponding ASCII
value is possible, use the built-in functions double and char.
>> num=double(x)
>> y=char(num)
num =
y=
109 97 116 104 32 54 50 49
math 621
The MATLAB built-in function strfun, with the help facility
provides a complete set of commands for working with
strings.
>> help strfun
8 / 26
Gemechu Fanta
Matrix Combination
General
char - Create character array (string).
double - Convert string to numeric character codes.
cellstr - Create cell array of strings from character array.
blanks - String of blanks.
deblank - Remove trailing blanks.
eval - Execute string with MATLAB expression
String Tests
ischar - True for character array (string).
iscellstr - True for cell array of strings.
isletter - True for letters of the alphabet.
isspace - True for white space characters.
9 / 26
Gemechu Fanta
Matrix Combination
String operations
strcat - Concatenate strings.
strvcat - Vertically concatenate strings.
strcmp - Compare strings.
strncmp - Compare first N characters of strings.
strcmpi - Compare strings ignoring case.
strncmpi - Compare first N characters of strings ignoring case.
String to number conversion
num2str - Convert number to string.
int2str - Convert integer to string.
mat2str - Convert matrix to evalable string.
str2double - Convert string to double precision value.
str2num - Convert string matrix to numeric array.
sprintf - Write formatted data to string.
sscanf - Read string under format control.
10 / 26
Gemechu Fanta
Matrix Combination
11 / 26
Gemechu Fanta
Matrix Combination
Gemechu Fanta
Matrix Combination
Gemechu Fanta
Matrix Combination
>> A = [1 0 2; 3 4 0]
A=
102
340
Components of a matrix are enclosed rectangular brackets, we use
semicolon ; to separate rows while space is used to separate
entries/components. MATLAB stores the above 2 x 3 matrix in the
variable A. If you want to call this matrix, just type A at the prompt
and then press enter.
14 / 26
Gemechu Fanta
Matrix Combination
Gemechu Fanta
Matrix Combination
Gemechu Fanta
Matrix Combination
Gemechu Fanta
Matrix Combination
Gemechu Fanta
Matrix Combination
Gemechu Fanta
Matrix Combination
>> Mt =
M
M=
135
042
203
renders the transpose of M .
The rank of a matrix can also be determined by using the
command rank
>> rank(M)
ans=
3
20 / 26
Gemechu Fanta
Matrix Combination
4. 3 Special Matrices
There are several special matrices in MATLAB, other than the empty
matrix [], with specific purpose, e.g. eye(m,n), zeros(m,n) &
ones(m,n)
>>zeros(2,3)
ans =
000
000
000
A matrix of zeros, basically used to allocated computer memory for
storage
21 / 26
>>ones(2,3)
ans =
111
111
Gemechu Fanta
1 1 1Introduction to Computing with MATLAB
Matrix Combination
A matrix of ones, basically used for the same purpose the matrix of
zeros.
>>eye(3)
ans =
100
010
001
an identity matrix of order 3.
For a square matrix, the MATLAB function rank can be used to
decide if it is singular or otherwise.
From linear algebra, a square matrix is singular provided it is rank
deficient, i.e. for an n x n matrix to be non-singular it should have a
rank of n.
Since a non-singular matrix has a non-zero determinant, the
MATLAB built-in function det that is used to compute the
determinant of a matrix is also helpful.
22 / 26
Gemechu Fanta
Matrix Combination
>>det(M)
ans =
-16
For a non-singular square matrix, the inverse can be computed using
the command inv
>>inv(M)
ans =
-0.7500 -0.2500 0.500
0.5625 0.4375 -0.3750
0.8750 0.1250 -0.2500
23 / 26
Gemechu Fanta
Matrix Combination
Matrix Combination
The basic operations of arithmetic (+, -, *, /), already recognized by
MATLAB can be used to combine two or more matrices.
A) Addition
If A and B are two matrices then we can add them to get a matrix C
provided that they have the same dimension, for instance if
2 1 3
A=
5 4 0
and
B=
0 1 1
2 3 1
Gemechu Fanta
Matrix Combination
>>A
A=
213
540
>>B
A=
0 -1 1
2 3 -1
>>C = A + B
C=
204
7 7 -1
25 / 26
Gemechu Fanta
Matrix Combination
26 / 26
>>E = A + D
??? Error using ==> +
Matrix
dimensions
must toagree.
Gemechu
Fanta
Introduction
Computing with MATLAB