Bubble.ro - because there is always something new to learn about

Introduction to Matlab

 

Category: Math software

If you are studying engineering or science, sooner or later, you will need to learn how to use Matlab, a programming language which allows easy manipulation of numerical data and implementation of algorithms. This tutorial will help you understand the basic syntax and data types used by the language.

  MATLAB stands for "matrix laboratory" and both the programming language and the environment which implements it have this name. It was written in C and it is developed by the Mathworks company.

  Most of the objects used in computations are, as it's name suggests, multi-dimensional matrixes. Any scalar value used is treated as a 1x1 matrix, vectors are treated as one dimensional matrixes, etc. Since all operations are optimized for matrix usage, you should get used to this style of programming if you plan on utilising Matlab extensively.

  General use

  In Matlab, you can enter commands on the command line or write full programs and run them. Entering a command followed by a semicolon will only execute the command, without printing the result. If it is not followed by a semicolon, the result will be printed in the command window.

  You can enter more than one command on one line, if you separate them by commas.

  For example, entering the line:

  2+3, 5+6

  will result in the answers 5 and 11, which will be assigned to a special Matlab variable, called ans, storing the results of operations. The variable will be also printed on the screen.

  If you enter:

  2+3;

  you will notice nothing is printed, but if you check the variable ans(just type ans) you will see that it's value is 5.

  Defining simple variables

 
The syntax used to define a new matrix is simple and intuitive. Elements on the same line are separated by blanks or commas and lines are separated by semicolons. For example:

  A = [1 4; 5 7]

  defines the matrix:

          1  4
  A =
          5  7

  Another option is to separate the lines by using enter:

  A = [1 4
         5 7]

  The variable is created when it is first used or initialized, so there is no need to declare it's type before using it, like in C.

  Once created, the variable will exist until you quit the program or you specifically delete it. You can see the list of existing variables by using the "who" command.

  You can also create new matrixes by merging existing ones, like in this example:

A = [1 4; 5 7]
B = [1 3; 4 6]
D = [A B]

which results in:

       1  4  1  3
D =
       5  7  4  6

  Like C, Matlab is case-sensitive, so be careful when using variable names. Matrix A is different from matrix a.

  Accessing matrix elements

 
To select matrix elements, you can use the index of an element between (). Elements are counted from top to bottom and left to right, first on columns, then rows. For example:

  a = A(3)

  results in a  = 4.

 In the case of multi-dimensional matrixes, you can use the indexes of lrows and columns to select the element, like this:

b = A(1, 2)

  will assign the value 4 to b. The first index is the row number and the second one is the column. Rows and columns are numbered starting with 1, not 0, like C arrays.

  If you need to use a number of rows or columns from a matrix, you can use the ":" operator,  like in this example:

d = [1 2 3; 4 5 6; 7 8 9]
e = d(:, 2:3)

will result in:

    2   3
e = 5   6
    8   9

  The first index gives the rows you want to select, and the second the columns. Used alone, the ":" means select all the rows or columns.

  Usual Matlab operations

The usual arithmetic operators used in Matlab are: +, -, *(for multiplying), / (for dividing). To get the transpose of a matrix, use operator ' .

  "*" and "/" perform matrix multiplication or division(which actually means  mutliplication with the inverse of the second matrix). The dimensions of the two operands must line up correctly.  If you want to perform mutliplication or division element by element, you must put a  "."  in front of the operator. In this case, the two operands must have the same dimensions.

  For example, let's define to matrixes, a and b, and a vector, c:

a = [1 2; 3 4; 5 6]
b = [1 2 3; 4 5 6]
c = [1 2 3]

The operation c'   will result in:
   
 
     1
ans = 2
      3

  a*b is:

       9   12  15
ans = 19  26  33
      29  40  51

  You can't use a.*b, because you'd get an error(the dimensions of the two matrixes don't match). Let's define another vector, d, to see how the element by element multiplication works.

  d = [1 2 3]

  Now, using:

  c.*d

you'll get:

ans = 1 4 9


That's about all for basic knowledge about Matlab. You should also know you can use the  control structures from C (for, while, if), along with a large number of specialized functions integrated by Matlab(abs, sqrt and so on). To get info about a specific function, you can use the help provided by the program, just by typing help function_name. If the function exists, the help will be displayed. Since the functions you need depend a lot on your specific area of study, these should be covered by separate tutorials.



Posted by: Quarc on August 10, 2006 at 12:14.
 

» Comments

More Introductory MATLAB Material
This is a great introduction to MATLAB. I wrote a short article on calculating basic statistical summaries in MATLAB, which can be found at the following address:

"Basic Summary Statistics in MATLAB"
http://matlabdatamining.blogspot.com/2007/04/basic-summary-statistics-in-matlab.html

Your readers might also be interested in the MATLAB code sources I mention here:

"Finding MATLAB Source Code And Tools"
http://matlabdatamining.blogspot.com/2006/11/finding-matlab-source-code-and-tools.html


-Will Dwinnell


Posted by Will Dwinnell on December 18, 2007 at 12:59 PM.

Random Article


Search


Feeds


Bubble.ro RSS Feed

All Categories


Articles


Aetolia - The Midnight Age
How to create the histogram of an image using PHP
How to convert an image to grayscale using PHP
How to check if an image is grayscale in PHP
Interchanging 2 variables without the use of a third
Error launching browser window:no XBL binding for browser
Convert the AOL user session collection to a MySQL database
Introduction to Matlab
Creating a customized session handling system in PHP (part II)
Creating a customized session handling system in PHP (part I)
Firefox crashing with Yahoo! Messenger
ADL Search for oDC
Video codecs explained
Browsershots
How to use Auto-Away Message with oDC
Create complete Windows XP disk with SP2 and all updates
Data Execution Prevention error message in Windows XP
Google Mars
Logarithmic scale graphs in Excel
Urban Dictionary (or wtf does l33t mean?)
Learn more about BIOS
Backup your Firefox and Thunderbird settings
Syndicate your Yahoo 360 profile
What is Google PageRank?
'Cannot Open the File: Mk:@MSITStore' Error Message
Get your Gmail with Mozilla Thunderbird
E-Books links
Change the size of your Explorer thumbnails
Remove previews from Windows Explorer
How can I turn off system beeps?
How do I disable Internet Explorer?
What are proxies or how do I protect my anonymity?
How to set aliases triggers or macros in MushClient
What is RSS?
Palm Zire 31 fast review
oDC Installation and Basic Configuration
How I built a 2x80W amplifier (using power modules)
Leech/HotLink Protection
How to block referrer detection?
How to find out your IP address
Getting started with Mushclient
What is spyware and how do I protect my PC from it?
Stumble Upon - random surfing around the web
Automatic file backup for Windows users
How can I read foreign language sites?
Protect your web surfing privacy!
What is BitTorrent?
No more ads! Adblock for Firefox
Why use Firefox instead of Internet Explorer?
How do I create my own Yahoo ID?
© Copyright 2006-2020 Bubble. All rights reserved. Sitemap - Contact