0% found this document useful (0 votes)
44 views

Introduction To Computing With MATLAB: Gemechu Fanta

The document provides an introduction to matrix combination in MATLAB. It discusses fundamental data types in MATLAB including numeric data types like integers and floating point numbers. It also discusses strings and conversion between numbers and strings. It then discusses various operations that can be performed on matrices in MATLAB, including selecting parts of a matrix, modifying matrices, and combining matrices.

Uploaded by

chandralove
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Introduction To Computing With MATLAB: Gemechu Fanta

The document provides an introduction to matrix combination in MATLAB. It discusses fundamental data types in MATLAB including numeric data types like integers and floating point numbers. It also discusses strings and conversion between numbers and strings. It then discusses various operations that can be performed on matrices in MATLAB, including selecting parts of a matrix, modifying matrices, and combining matrices.

Uploaded by

chandralove
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Matrix Combination

Introduction to Computing with MATLAB

Gemechu Fanta

Kotebe Teachers College of Education


August 13, 2012

1 / 26

Gemechu Fanta

Introduction to Computing with MATLAB

Matrix Combination

outline of lecture 2

Data Types in MATLAB


I
I
I

Fundamental data types


Strings
Conversion to Strings

Operations on Matrices
I
I
I
I
I

2 / 26

Selecting Part(s) of a Matrix


Modifying a Matrix
Special Matrices
Matrix Combination
Systems of Linear Equations

Gemechu Fanta

Introduction to Computing with MATLAB

Matrix Combination

3. Data Types in MATLAB


The MATLAB system originally developed as a high level
interface to a et of numerical computing packages based on
LINPACK and the EISPACK routines has the basic data type, a
two dimensional matrix/array ontaining real or complex floating
point numbers.
LINPACKis a software library for performing numerical linear
algebra on gital computers. It was written in Fortran by Jack
Dongarra and et al.
LINPACK makes use of the BLAS (Basic Linear Algebra
Subprograms) braries for performing basic vector and matrix
operations.
EISPACK is a software library for numerical computation of
eigenvalues nd eigenvectors of matrices, written in FORTRAN.
It contains subroutineor calculating the eigenvalues of nine
classes of matrices:
3 / 26

Gemechu Fanta

Introduction to Computing with MATLAB

Matrix Combination

Data Types in MATLAB...


The smallest size of such a matrix is 0x0 i.e. an empty matrix [] and
can vary form 1x1 matrix (a scalar) to an n-dimensional array of any
size.
Character array which is another data type supported by MATLAB
environment is used to store text data.
3.1 Fundamental data types There are about 15 fundamental
data types in MATLAB. Each of which is in the form of an array, a
matrix. All of the fundamental data types are indicated with oval
circle in the following diagram.
colorredNumeric data types in the MATLAB system include
signed and unsigne integers, and single & double-precision
floating point numbers.
By default, MATLAB stores all numeric values as
double-precision floating point.
4 / 26

Gemechu Fanta

Introduction to Computing with MATLAB

Matrix Combination

Fundamental data types...

You can choose to store


any number, or array of
numbers as integers i.e.
as single-precision.
Integer and
single-precision arrays
offer more efficient
memory storage than
double-precision.

5 / 26

Gemechu Fanta

Introduction to Computing with MATLAB

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

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

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

Introduction to Computing with MATLAB

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

Introduction to Computing with MATLAB

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

Introduction to Computing with MATLAB

Matrix Combination

Base number conversion


hex2num - Convert IEEE hexadecimal to double precision
numberhex2dec - Convert hexadecimal string to decimal integer.
dec2hex - Convert decimal integer to hexadecimal string.
bin2dec - Convert binary string to decimal integer.
dec2bin - Convert decimal integer to binary string.
base2dec - Convert base B string to decimal integer.
dec2base - Convert decimal integer to base B string.

11 / 26

Gemechu Fanta

Introduction to Computing with MATLAB

Matrix Combination

3.3 Conversion to Strings


MATLAB has built-in functions, num2str and int2str for general
purpose conversion of numbers to strings.
The function int2str converts integer to string while num2str
converts any number to string.
The latter allows format specification. Consider dividing the
number by 4.
Whereas, direct computation returns the answer with only 4
significant digits, as shown bellow
>> x=pi/4 >>y=num2str(pi/4,% 12.6e)
x=
y=
0.7854
7.853982e-001
Format specification returns the desired result as per specified
parameters,
12 / 26

Gemechu Fanta

Introduction to Computing with MATLAB

Matrix Combination

The control of formatting involves the character precision fields.


Thus to display six decimal places in a field of 12 characters with
exponential notation, we have to type it as in the above
command.
4. Operations on Matrices Recall, a matrix is a rectangular array
of numbers with a given number of rows and columns, an m x n
matrix has m rows and n columns. One can enter, for instance a 2 x
3 matrix,

by typing either on a command line, on the editor window or directly


at the MATLAB prompt, if it is entered at the prompt then pressing
enter will produce the next result
13 / 26

Gemechu Fanta

Introduction to Computing with MATLAB

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

Introduction to Computing with MATLAB

Matrix Combination

4.1 Selecting Part(s) of a Matrix


In MATLAB the component on the row i and column j of a
matrix A, i.e. the Aij entry is denoted by A(i,j). One can
select/extract the component Aij by typing A(i,j) at the prompt
and then pressing return
>> A(1,3)
ans=
2
A row of a matrix can be selected by specifying the row and
then using the column : symbol as A(i,:). Here i specifies the
row while the : allows to select all components along the
specified row.
>> A(2,:)
ans=
340
15 / 26

Gemechu Fanta

Introduction to Computing with MATLAB

Matrix Combination

Here MATLAB returns the second row and stores it in a


temporary variable ans. Alternatively, we can define our own
variable, say A 2 and store the second row in this variable as
shown bellow
>> A 2= A(2,:)
A 2=
340
A column of a matrix can also be picked in similar fashion as
that of a row except for the interchange of a roll between colon :
and the number i. Thus, the jth column of a matrix is selected
by entering A(:,j).
>> A3 = A(:,3)
A3 =
2
0
The colon operator : might be interpreted as either all rows or
all columns.
16 / 26

Gemechu Fanta

Introduction to Computing with MATLAB

Matrix Combination

The symbol :colon can also be used to extract sub-matrix of a


matrix, e.g.
>> B = A(:,1:2)
B=
10
34
or may be used to convert a matrix into a vector, e.g.
>> C = A(:)
C=
1
3
0
4
2
0
17 / 26

Gemechu Fanta

Introduction to Computing with MATLAB

Matrix Combination

4.2 Modifying a Matrix


We can change components of a matrix by mere assignment.
Consider the matrix M
>> M = [ 1 0 2;3 4 0;5 2 3] If we want to change >>M
M=
M32, say to 9 then
M=
102
we enter
102
340
>>M(3,2)=9; Call M 3 4 0
523
to check the change 5 9 3
A row/column of a matrix can be deleted by empty-assignment.
To delete the second row of M , we do the following
>>M(2,:)=[] >>M(:,3)=[]
M=
M=
102
10
593
34
52
18 / 26

Gemechu Fanta

Introduction to Computing with MATLAB

Matrix Combination

MATLAB has built-in functions with which we may


identify/determine properties of a matrix,
>> size(M)
ans=
33
or more precisely
>>[m n]=size(M)
m=
3
n=
3
Tells us the dimension of the matrix we are working with, while
>> D = diag(M)
D=
1
4
3 Gives the diagonal compoents of M and
19 / 26

Gemechu Fanta

Introduction to Computing with MATLAB

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

Introduction to Computing with MATLAB

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

Introduction to Computing with MATLAB

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

Introduction to Computing with MATLAB

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

then, once we enter the matrices A & B as explained thus far, we


simply type C=A+B at the prompt and press return, as always.
24 / 26

Gemechu Fanta

Introduction to Computing with MATLAB

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

Introduction to Computing with MATLAB

Matrix Combination

If the two matrices have different dimensions then


MATLAB send an error message
Example
>>A = [2 1 3;5 4 0]
A=
213
540
>>D = [0 -1 1;2 3 -1; 1 0 1]
D=
0 -1 1
2 3 -1
101

26 / 26

>>E = A + D
??? Error using ==> +
Matrix
dimensions
must toagree.
Gemechu
Fanta
Introduction
Computing with MATLAB

You might also like