Lecture2b
Lecture2b
who;
It is generally good programming style to write only one command per line; however, MATLAB
does let you put multiple commands on a line.
x = 5; y = 13; w = 2*x + y; who;
More commonly one wishes to continue a single command across multiple lines due to the
length of the syntax. This can be accomplished by using three dots.
z = 2*x + ...
y
Finally, when using clear we can get rid of all of the variables at once with the command "clear
all".
clear all;
who; It does not print out anything because there are no variables.
The simplest, but NOT RECOMMENDED, way to declare a variable is by entering the
components one-by-one.
x(1) = 1;
x(2) = 4;
x(3) = 6;
x display contents of x
It is generally better to declare a vector all at once, because then MATLAB knows how much
memory it needs to allocate from the start. For large vectors, this is much more efficient.
y = [1 4 6] does same job as code above
Note that this declares a row vector. To get a column vector, we can either use the transpose
(adjoint for complex x) operator xT = x'; takes the transpose of the real row vector x or, we
can make it a column vector right from the beginning
yT = [1; 4; 6];
To see the difference in the dimensions of a row vs. a column vector, use the command "size"
that returns the dimensions of a vector or matrix.
size(xT)
size(y)
size(yT)
The command length works on both row and column vectors.
length(x), length(xT)
We can also use the . operator to tell MATLAB to perform a given operation on an element-by-
element basis. Let us say we want to set each value of y such that y(i) = 2*x(i) + z(i)^2 + 1.
We can do this using the code
y = 2.*x + z.^2 + 1
The dot and cross products of two vectors are calculated by
dot(x,y)
z=cross(x,y)
We can define a vector also using the notation [a : d : b]. This produces a vector a, a + d, a +
2*d, a + 3*d, ... until we get to an integer n where a + n*d > b. Look at the two examples.
v = [0 : 0.1: 0.5];
v2 = [0 : 0.1: 0.49];
If we want a vector with N evenly spaced points from a to b, we use the command
"linspace(a,b,N)".
v2 = linspace(0,1,5)
Sometimes, we will use a vector later in the program, but want to initialize it at the beginning
to zero and by so doing allocate a block of memory to store it. This is done by
v = linspace(0,0,100)'; allocate memory for column vectors of zero
Finally, we can use integer counting variables to access one or more elements of a matrix.
v2 = [0 : 0.01 : 100];
c=v2(49)
w = v2(65:70)
clear all
For a complex matrix, ' returns the adjoint (transpose and conjugate. The conjugation
operation is removed by using the "transpose only" command .'
E = D;
E(1,2) = E(1,2) + 3*i;
E(2,1) = E(2,1) - 2*i;
E', E.'
The "who" command lists the matrices in addition to scalar and vector variables.
who
If in addition we want to see the dimensions of each variable, we use the "whos" command.
This tells use the size of each variable and the amount of memory storage that each requires.
whos
We can create a matrix with m rows and n columns, all containing zeros by
m=3; n=4;
C = zeros(m,n)
We create an Identity matrix, where all elements are zero except for those on the principle
diagonal, which are one.
D = eye(5)
Finally, we can use the . operator to perform element-by-element operations just as we did for
vectors. The following command creates a matrix C, such that C(i,j) = 2*A(i,j) + (B(i,j))^2.
C = 2.*A + B.^2
Matrices are cleared from memory along with all other variables.
clear A B
whos
clear all
who