next up previous
Next: Special constants Up: MATLAB summary Previous: Some general purpose commands

MATLAB variables


MATLAB deals with variables that may be scalars or arrays. The names of variables should begin with a letter, followed by letters, the digits 0-9 or the underscore symbol (_). Note that MATLAB is case-sensitive, so that, for example, the variables Aa and AA are different. Scalars can be entered using a number of different formats. The following all give the same result:
>> x=1520
x =
        1520
>> x=1.52e3
x =
        1520
>> x=15.2e2
x =
        1520
>> x=15200e-1
x =
        1520
>>
Thus 1.52e3 means 1.52 by 10 to the power 3.

Vectors (i.e. one-dimensional arrays) are set up as follows:

col2=[1; 2; 3; 4; 5; 6]: enter a column vector with entries 1,2,3,...

col3=[1 2 3 4 5 6]: enter a row vector with entries 1,2,3,...

xx=0 : 0.1 : 2.5: creates a vector with elements [0, 0.1,0.2, ..., 2.5].

More generally, two-dimensional arrays, or matrices, are entered as

>> a=[1 2;3 4]
a =
     1     2
     3     4
>> b =[ 5 6; 7 8]
b =
     5     6
     7     8

where elements in a given row are separated by spaces (or commas) and rows are separated by semi-colons.Alternatively use

>> b = [ 5 6
         7 8]
b =
     5     6
     7     8
These matrices can be combined as follows:
>> c = [ a b]    
c =
     1     2     5     6
     3     4     7     8
>> d = [a;b] 
d =
     1     2
     3     4
     5     6
     7     8
>>



Charlie Macaskill
Fri Mar 5 16:05:28 EST 1999

Copyright © University of Sydney 2003