MATLAB "help" is very useful. In the command window type, help commandwhere command is any MATLAB command.
If you do not know the lookfor keywordwhere keyword is a word related to the command. |
MATLAB variables are arrays of numbers. An array consisting of one element is called a scalar. For example;
x =2x has the value 2. More commonly, x will assume a number of values, say from 0 to 5, For example;
x = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]Note, 1:0.5:5 creates an array of numbers from 1 to five in steps of 0.5.
Examples of arrays
A = [1, 2; 3, 4; 5, 6]Creates a 3x2 array, 3 rows and 2 columns. The semicolon creates a new row.
A = 1 2 3 4 5 6
x = [4; 5]Creates an array with 2 rows and one column.
x = 4 5
If we multiply A by x, the rules of matrix algebra apply. That is,
A*x is an array with 3 rows and one column.A*x = 1*4 + 2*5 = 14 3*4 + 4*5 32 5*4 + 6*5 50
Many times matrix multiplication is not desired. For example, when we want the square in each element in an array.
x = 0:1:4Note that .* is used to denote element by element operation. The result is;y = x .* x
x= [0 1 2 3 4]Alsoy = [0 1 4 9 16]
stem(x,y)You can add axis labels and a title to stem plots.
MATLAB echos back all input and values generated. The semicolon, ;, suppresses this output. For example;
x = 1:.01:10;generates an array x with 100 elements, but the array is not echoed back.
zeros(4,5) is an array with 4 rows and 5 columns where every element is 0ones(2,3) is an array with 2 rows and 3 columns where every element is 1
Consider the 3x4 array
A = 1 2 3 4 5 6 7 9 0 9 3 1The array has 3 rows and 4 columns. Any individual element can be extracted from the array. For example,
x = A(2,3);results in a value of 7 for x.
The command
D = A(:,3);produces the colunm array D
D = 3 7 3
The command
y = A([1   2],[2   4])results in the array,
y = 2 4 6 9y consists of rows 1 and 2 and columns 2 and 4 of the array A.
Consider the file called bipolar with the following content,
The MATLAB command load bipolar loads the data from the file bipolar into an array called bipolar.0.63 1.00E-006 1.97E-004 0.65 2.60E-006 5.19E-004 0.68 6.50E-006 1.34E-003 0.7 1.90E-005 3.90E-003 0.73 5.60E-005 1.16E-002 0.75 4.97E-004 8.02E-002 0.78 8.98E-004 1.07E-001 0.8 1.72E-003 1.32E-001
The MATLAB command,
Ib = bipolar(:,2);creates a one column array called Ib.
Ib = 1.00E-006 2.60E-006 6.50E-006 1.90E-005 5.60E-005 4.97E-004 8.98E-004 1.72E-003The second column of bipolar is equal to the new one column array Ib.