The basic data type in Matlab is an n-dimensional array of double precision numbers. Matlab 5 differs from earlier versions of Matlab in that other data types are supported (also in the fact that general, multi-dimensional arrays are supported; in earlier versions, every variable was a two-dimensional array (matrix), with one-dimensional arrays (vectors) and zero-dimensional arrays (scalars) as special cases). The new data types include structures (much like structures in the C programming language--data values are stored in named fields), classes, and ``cell arrays'', which are arrays of possibly different data types (for example, a one-dimensional array whose first entry is a scalar, second entry a string, third entry a vector). I mostly discuss the basic features using n-dimensional arrays, but I briefly discuss the other data types later in the paper.
The following commands show how to enter numbers, vectors and matrices,
and assign them to variables (
>> is the
Matlab prompt on
my computer; it may be different with different computers or different
versions of Matlab. I am using version 22.214.171.12484. On my Unix workstation,
I start Matlab by typing matlab at the Unix prompt.):
>> a = 2 a = 2 >> x = [1;2;3] x = 1 2 3 >> A = [1 2 3;4 5 6;7 8 0] A = 1 2 3 4 5 6 7 8 0Notice that the rows of a matrix are separated by semicolons, while the entries on a row are separated by spaces (or commas).
A useful command is ``whos'', which displays the names of all defined variables and their types:
>> whos Name Size Bytes Class A 3x3 72 double array a 1x1 8 double array x 3x1 24 double array Grand total is 13 elements using 104 bytesNote that each of these three variables is an array; the ``shape'' of the array determines its exact type. The scalar a is a array, the vector x is a array, and A is a array (see the ``size'' entry for each variable).
One way to enter a n-dimensional array (n>2) is to concatenate two or more (n-1)-dimensional arrays using the cat command. For example, the following command concatenates two arrays to create a array:
>> C = cat(3,[1,2;3,4;5,6],[7,8;9,10;11,12]) C(:,:,1) = 1 2 3 4 5 6 C(:,:,2) = 7 8 9 10 11 12 >> whos Name Size Bytes Class A 3x3 72 double array C 3x2x2 96 double array a 1x1 8 double array x 3x1 24 double array Grand total is 25 elements using 200 bytesNote that the argument ``3'' in the cat command indicates that the concatenation is to occur along the third dimension. If D and E were arrays, the command
>> cat(4,D,E)would create a array (try it!).
Matlab allows arrays to have complex entries. The complex unit is represented by either of the built-in variables i or j:
>> sqrt(-1) ans = 0 + 1.0000iThis example shows how complex numbers are displayed in Matlab; it also shows that the square root function is a built-in feature.
The result of the last calculation not assigned to a variable is automatically assigned to the variable ans, which can then be used as any other variable in subsequent computations. Here is an example:
>> 100^2-4*2*3 ans = 9976 >> sqrt(ans) ans = 99.8799 >> (-100+ans)/4 ans = -0.0300The arithmetic operators work as expected for scalars. A built-in variable that is often useful is :
>> pi ans = 3.1416
Above I pointed out that the square root function is built-in; other common scientific functions, such as sine, cosine, tangent, exponential, and logarithm are also pre-defined. For example:
>> cos(.5)^2+sin(.5)^2 ans = 1 >> exp(1) ans = 2.7183 >> log(ans) ans = 1Other elementary functions, such as hyperbolic and inverse trigonometric functions, are also defined.
At this point, rather than providing a comprehensive list of functions available in Matlab, I want to explain how to get this information from Matlab itself. An extensive online help system can be accessed by commands of the form help <command-name>. For example:
>> help ans ANS The most recent answer. ANS is the variable created automatically when expressions are not assigned to anything else. ANSwer. >> help pi PI 3.1415926535897.... PI = 4*atan(1) = imag(log(-1)) = 3.1415926535897....
A good place to start is with the command help help, which explains how the help systems works, as well as some related commands. Typing help by itself produces a list of topics for which help is available; looking at this list we find the entry ``elfun--elementary math functions.'' Typing help elfun produces a list of the math functions available. We see, for example, that the inverse tangent function (or arctangent) is called atan:
>> pi-4*atan(1) ans = 0
It is often useful, when entering a matrix, to suppress the display; this is done by ending the line with a semicolon (see the first example in the next section). The command more can be used to cause Matlab to display only one page of output at a time. Typing more on causes Matlab to pause between pages of output from subsequent commands; as with the Unix ``more'' command, a space character then advances the output by a page, a carriage return advances the output one line, and the character ``q'' ends the output. Once the command more on is issued, this feature is enabled until the command more off is given.