DIP Unit I
DIP Unit I
MATLAB stands for Matrix Laboratory. MATLAB was written initially to implement a
simple approach to matrix software developed by the LINPACK (Linear system package)
and EISPACK (Eigen system package) projects.
As its name contains the word Matrix, MATLAB does its' all computing based on
mathematical matrices and arrays. MATLAB's all types of variables hold data in the form
of the array only, let it be an integer type, character type or String type variable.
The development of the MATLAB started in the late 1970s by Cleve Moler, the chairman
of the Computer Science department at the University of New Mexico. Cleve wanted to
make his students able to use LINPACK & EISPACK (software libraries for numerical
computing, written in FORTRAN), and without learning FORTRAN. In 1984, Cleve Moler
with Jack Little & Steve Bangert rewrote MATLAB in C and founded MathWorks. These
libraries were known as JACKPAC at that time, later these were revised in 2000 for matrix
manipulation and named as LAPACK.
Advantages of MATLAB
Ease of Use
Platform Independence
Predefined Functions
MATLAB comes complete with a huge library of predefined functions that provides
tested and prepackaged solutions to many primary technical tasks. For example,
suppose that we are writing a program that must evaluate the statistics associated with
an input data set. In most languages, we would need to write our subroutines or
functions to implement calculations such as the arithmetic mean, standard deviation,
median, and so on. These and hundreds of other services are built right into the
MATLAB language, making your job much more comfortable.
In addition to the vast libraries of services built into the basic MATLAB language, there
are many special-purpose toolboxes applicable to help solve complex problems in
particular areas. For example, a user can buy standard toolkits to solve problems in
signal processing, control systems, communications, image processing, and neural
networks, etc. There is also a broad compilation of free user-contributed MATLAB
programs that are shared through the MATLAB Web site.
Device-Independent Plotting
MATLAB has many basic plotting and imaging commands. The plots and pictures can be
displayed on any graphical output device provided by the computer on which MATLAB
is running. This facility makes MATLAB an outstanding tool for visualizing technical
information.
Graphical User Interface
MATLAB Compiler
A separate MATLAB compiler is available. This compiler can compile MATLAB programs
into a real executable that runs faster than the interpreted code. It is a great technique
to convert a prototype MATLAB program into an executable suitable for sale and
distribution to users
Disadvantage of MATLAB
There is two major disadvantage of MATLAB programming language
Interpreted language
The first disadvantage is that it is an interpreted language and, therefore, may execute
more slowly than compiled language. This problem can be check by properly structuring
the MATLAB program.
Cost
A full copy of MATLAB is five to ten times more costly than a conventional C or
FORTRAN compiler. This comparatively high cost is more than offset by the decreased
time necessary for an engineer or scientist to create a working program, so MATLAB is
cost-effective for businesses. However, it is too expensive for most individuals to
consider purchasing. Fortunately, there is also an inexpensive Student Edition of
MATLAB, which is an excellent tool for students wishing to learn the language. The
Student Edition of MATLAB is virtually identical to the full edition.
MATLAB Environment
The structural unit of data in any MATLAB program is the array. An array is a collection
of record values organized into rows and columns and known by a single name.
Individual data values within an array may be accessed by including the name of the
array, followed by subscripts in parentheses that identify the row and column of the
particular value. Even scalars are acted as arrays by MATLAB: they are arrays with only
one row and one column. When MATLAB executes, it can display various types of
windows that accept commands or display data.
MATLAB Desktop
When we start MATLAB Version 7.4, a specific window known as the MATLAB desktop
appears. The desktop is a window that includes other windows display MATLAB data,
plus toolbars and a "Start" button similar to that used by Windows XP. By default, most
MATLAB tools are "docked" to the desktop so that they develop inside the desktop
window. However, the user can choose to "undock" any or all tools, making them
appear in windows separate from the desktop.
It integrates various tools for managing data, variables, and applications within the
MATLAB environment.
The main tools within or accessible from the MATLAB desktop are
o Command Window
o Command History Window
o Start Button
o Documents Window, containing the Editor/Debugger and the Array Editor
o Figure Windows
o Workspace Browser
o Help Browser
o Path Browser
Command Window
This is the main window. It is characterized by the MATLAB command prompt (>>).
When you launch the function program, MATLAB puts you in this window. All
commands containing those for running user-written programs are typed in this window
at the MATLAB prompt. In MATLAB, this window is a part of the MATLAB that contains
other smaller windows or panes.
Here is an image of the screen that appears when you open the MATLAB.
o On the left side, inside the command window, there is something visible in small
fonts, and that is fx, an abbreviated form for function.
o When you click on the fx, a drop-down search bar gets opened.
o As shown in the image, these all are the name of the products we have installed.
o Click on any of the products to get a list of all the related functions.
o 'Browse for Functions' is a function search pop-up, and we can browse for the
functions related to all the installed products.
o The keyboard shortcut for the function search pop-up is Shift + F1.
i)
o Whenever we enter any character or word or sentence and press enter, it will
show error like this-Undefined function or variable 'jdjdkjdk45'.
o So the text should be enclosed either in double quotes (" ") or single quotes (' ').
ii)
Since all types of data in MatLab are stored in the form of arrays, MatLab provides
two different kinds of arithmetic operations, i.e. array operations and matrix
operations. MatLab array operations are just ordinary arithmetic operations that
supports multidimenstional arrays for processing element by elemt operations.
While MatLab matrix operation are ordinary matrix operations that following the
rules of linear algebra. Both array operations and matrix operations share the
similar symbols of operation, a period characater, "." is used to distinguish the array
operations from the matrix operations. However, as the addition and subtraction
for both matrix operation and array operation are the same, the period characater,
"." is not necessary and the character pairs ".+" and ".-" are not used. Besides, the 1-
by-1 array, scalar, is also a special type of MatLab array. A 1-by-1 array, scalar can
have array operation with an array of any size also. A 1-by-1 matrix, scalar can also
have martrix operation with a matrix of any size, but limited by the matrix
multiplication, the 1-by-1 matrix, scalar can only be the divisor of the right and left
division.
Element-Wise Array
Matrix
Arithmetic Arithmetic
Arithmetic Operations
Operators Operations
Power
A^B Matrix Power A.^B Array Power
^
The + addition operator of the expression A+B means adds matrix B to matrix A.
Since only a scalar can be added by or added to a matrix of any size, unless either A
or B is a scalar, A and B must have the same size.
Examples
m-by-n matrix A + m-by-n matrix B
scalar S + matrix A + scalar S
+ Unary Plus
The structural unit of data in any MATLAB program is the array. An array is a collection
of record values organized into rows and columns and known by a single name. Arrays
can be allocated as either vectors or matrices. The term "vector" is generally used to
define an array with only one dimension, while the term "matrix" is usually used to
determine an array with two or more dimensions.
The numbers of the row state the size of an array, and the numbers of the column in the
array, with the numbers of the row, mentioned first. The total number of items in the
array will be the product of the number of rows and the number of the column.
M
Facebook Temporarily Allows Violent Anti-Russian Posts
MATLAB variables name must start with a letter, followed by any sequence of letters,
numbers, and the underscore (_) character. Only the first 63 characters are essential; if
more than 63 are used, the remaining characters will be ignored. If two variables are
stated with names that only differ in the 64th character, MATLAB will treat them as the
same variable.
Creating and Initializing Variables in MATLAB
MATLAB variables are automatically generated when they are initialized.
var = expression;
where var is the name of the variables and expression is a scalar constant, an array, or a
combination of constants, other variables, and mathematical operations (+, -, etc.). The
value of the expression is computed using the standard rules of mathematics, and the
resulting values are saved in the named variable. The semicolon at the last of the
statement is optional. If the semicolon is absent, the values assigned to var will be
echoed in the command window. If it is present, nothing will be shown in the Command
Window, even though the assignment has appeared.
var = 40i;
var2 = var/5;
x = 1; y = 2;
array = [1 2 3 4];
The first example generates a scalar variable of type double and saves the imaginary
number 40i in it.
The second example generates a scalar variable and saves the result of the expression
var/5 in it.
The third example shows that multiple assignment statements can be placed on a single
line, supported that they are divided by semicolons or commas.
The last example display that variables can also be initialized with arrays of data. Such
arrays are build up using brackets ([]) and semicolons. All of the items of an array are
listed in row order. In other words, the value in each row are recorded from left to the
right, with the top-most row first, and the bottom-most row last. The single value within
a row are separated by blank spaces or commas, and the rows themselves are divided
by semicolons or newlines.
The following statements are all legal arrays that can be used to initialize a variable:
The number of items in every row of an array must be the same, and the number of
items in every column must be the same. An expression such as
[1 2 3; 4 5];
is illegal because row 1 has three items, while row 2 has only two items.
Initializing with Built-In Functions
Arrays can also be initialize using built-in MATLAB function. For example, the function
zero can be used to generate an all-zero array of any desired size. There are a various
form of the zeros function. If the function has an individual scalar argument, it will
develop a square array using the single arguments as both the number of rows and the
number of columns. If the function has two scalar argument, the first arguments will be
the number of rows, and the second arguments will be the number of the columns.
Since the size function return two values including the number of row and column in an
array, it can be combined with the zero function to create an array of zeros that is the
same size of another array.
a = zeros(2);
b = zeros(2,3);
c = [1 2; 3 4];
d = zeros(size(c));
Similarly, the ones function can be used to generate array including all ones, and the
eye function can be used to generate arrays including identity matrices, in which all
on-diagonal items are one, while all off-diagonal items are zero.
Following the tables containing a list of standard MATLAB functions useful for initializing
variables.
Functions Purpose
size(arr) Return two values specifying the number of rows and columns
in arr.
When this function is executed, MATLAB prints out the string 'Enter an input value:,' and
then waits for the client to respond. If the customer enters a single number, it may just
be typed in. If the customer enter an array, it must be enclosed in brackets. In other
case, whatever is typed will be stored in variable my_val when the return key is entered.
If only the return key is entered, then an empty matrix will be generated and stored in
the variable.
If the input function contains the character 's' as a second argument, then the input data
is returned to the customer as a character string. Thus, the statement
Multidimensional Arrays
Try This ExampleCopy Command Copy Code
A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two
dimensions are represented by rows and columns.
Each element is defined by two subscripts, the row index and the column index. Multidimensional arrays
are an extension of 2-D matrices and use additional subscripts for indexing. A 3-D array, for example,
uses three subscripts. The first two are just like a matrix, but the third dimension
represents pages or sheets of elements.
1 2 3
4 5 6
7 8 9
Now add a second page. To do this, assign another 3-by-3 matrix to the index value 2 in the third
dimension. The syntax A(:,:,2) uses a colon in the first and second dimensions to include all rows and
all columns from the right-hand side of the assignment.
A(:,:,2) = [10 11 12; 13 14 15; 16 17 18]
A =
A(:,:,1) =
1 2 3
4 5 6
7 8 9
A(:,:,2) =
10 11 12
13 14 15
16 17 18
The cat function can be a useful tool for building multidimensional arrays. For example, create a new 3-D
array B by concatenating A with a third page. The first argument indicates which dimension to concatenate
along.
B = cat(3,A,[3 2 1; 0 9 8; 5 3 7])
B =
B(:,:,1) =
1 2 3
4 5 6
7 8 9
B(:,:,2) =
10 11 12
13 14 15
16 17 18
B(:,:,3) =
3 2 1
0 9 8
5 3 7