E5 MatLab - 2nd class demo
Contents
This file (both .html and .m script) are available on E5 web site. From the schedule, click on the link for "Lab 6 files" located on the schedule (in the week 6 spot, not surprisingly). Last week's files are also here.
some special matrices
The identity matrix - note variable name at the left
I=eye(3)
I =
1 0 0
0 1 0
0 0 1
A matrix of zeros (by default, square)
Z=zeros(3)
Z =
0 0 0
0 0 0
0 0 0
A non-square matrix of zeros
Z=zeros(2,5)
Z =
0 0 0 0 0
0 0 0 0 0
Can also create a matrix of ones...
O=ones(3)
O =
1 1 1
1 1 1
1 1 1
We can make the matrix the same size as another array
O=ones(size(Z))
O =
1 1 1 1 1
1 1 1 1 1
Define a matrix of random numbers
R=rand(2,5)
R =
0.7948 0.3786 0.5328 0.9390 0.5502
0.6443 0.8116 0.3507 0.8759 0.6225
Working with arrays and subsets of arrays
A=[1 -2 3 -4; -2 3 -4 5; -2 -4 -6 -8]
A =
1 -2 3 -4
-2 3 -4 5
-2 -4 -6 -8
find size
size(A)
ans = 3 4
find number of rows (the first dimension of the A matrix)
size(A,1)
ans = 3
what is the 2nd row?
A(2,:)
ans = -2 3 -4 5
what is the 3rd column?
A(:,3)
ans =
3
-4
-6
Define an array of zeros same size as a row of A (1 row, same number of columns)
z=zeros(1, size(A,2))
z = 0 0 0 0
now change the last row
A(3,:)=z
A =
1 -2 3 -4
-2 3 -4 5
0 0 0 0
Higher dimensional arrays
X=rand(2,4,3)
X(:,:,1) =
0.2305 0.1948 0.1707 0.4357
0.8443 0.2259 0.2277 0.3111
X(:,:,2) =
0.9234 0.1848 0.9797 0.1111
0.4302 0.9049 0.4389 0.2581
X(:,:,3) =
0.4087 0.2622 0.7112 0.1174
0.5949 0.6028 0.2217 0.2967
Take "top" array
X(:,:,1)
ans =
0.2305 0.1948 0.1707 0.4357
0.8443 0.2259 0.2277 0.3111
Take 3rd column of "middle" array
X(:,3,2)
ans =
0.9797
0.4389
Take first two elements of 2nd row of "bottom" array
X(2,1:2,3)
ans = 0.5949 0.6028
Using a function
p=3 % Define a variable "p" for no real reason.
p = 3
Define x and y values of a complicated shape (a smile)
x1Smile=linspace(-0.5,0.5); x2Smile=linspace(0.5,-0.5);
y1Smile=0.8*x1Smile.^2+0.1; y2Smile=01.2*x2Smile.^2;
xSmile=[x1Smile x2Smile]; ySmile=[y1Smile y2Smile];
smile=patch(xSmile,ySmile,[1 0 0]); axis([-2 2 -2 2],'square')
Find the perimeter
smilePerimeter=perim(xSmile,ySmile)
smilePerimeter = 2.3026
Note that the variable p is unchanged
p
p = 3
Graphical transformations
Rotation
Define a point in 2-d space as a column vector (x,y)=(1,-2)
x=1; y=-2;
Draw line from the origin with a blue dotted line.
plot([0 x],[0 y],'b:');
add a blue circle at end, without erasing.
hold on; plot(x, y, 'bo');
Now add labels, scale axes and add grid.
xlabel('x'); ylabel('y'); title('The point (x,y) = (1,-2)'); axis([-3 3 -3 3], 'square'); grid on
Define transformation matrix for a -90 degree rotation
theta = -90 * pi/180; T=[cos(theta) sin(-theta) 0; sin(theta) cos(theta) 0; 0 0 1]
T =
0.0000 1.0000 0
-1.0000 0.0000 0
0 0 1.0000
Perform rotation and plot Note, for the transformations we add a third row that is always 1
xy=[x; y; 1]
xy =
1
-2
1
xyp=T*xy
xyp =
-2.0000
-1.0000
1.0000
xp=xyp(1) yp=xyp(2)
xp =
-2
yp =
-1.0000
Plot
plot([0 xp],[0 yp],'b-','Linewidth',2); plot(xp,yp,'bo','MarkerFaceColor','b'); title('The point (1,-2) rotated by -90^\circ') hold off; %Next plot will clear the screen
Translation
Now for a translation, tx=1, ty=4
tx=1; ty=4; T=[1 0 tx; 0 1 ty; 0 0 1]
T =
1 0 1
0 1 4
0 0 1
do transformation
xyp=T*xy xp=xyp(1); yp=xyp(2);
xyp =
2
2
1
Plot both points
plot([0 x],[0 y],'b:'); hold on; plot(x, y, 'bo'); xlabel('x'); ylabel('y'); plot([0 xp],[0 yp],'b-','Linewidth',2); plot(xp,yp,'bo','MarkerFaceColor','b'); title('The point (1,-2) translated by t_x=1.0, t_y=4'); axis([-3 3 -3 3],'square'); grid on hold off; %Next plot will clear the screen
Combined rotation and translation
theta=-90*pi/180; tx=1; ty=4; T=[cos(theta) sin(-theta) tx; sin(theta) cos(theta) ty; 0 0 1]
T =
0.0000 1.0000 1.0000
-1.0000 0.0000 4.0000
0 0 1.0000
do transformation
xyp=T*xy xp=xyp(1); yp=xyp(2);
xyp =
-1
3
1
Plot both points
plot([0 x],[0 y],'b:'); hold on; plot(x, y, 'bo'); xlabel('x'); ylabel('y'); plot([0 xp],[0 yp],'b-','Linewidth',2); plot(xp,yp,'bo','MarkerFaceColor','b'); title('(1,-2) Rotation: -90^\circ, Translation: t_x=1, t_y=4'); axis([-3 3 -3 3], 'square'); grid on; hold off; %Next plot will clear the screen
Manipulating graphical objects (patches)
Clear the figure, set axis limits, grid
clf; axis([-3 3 -3 3], 'square'); grid on;
Define a patch by its vertices
x=[0 2 2]; y=[0 1 0];
myTriangle=patch(x,y,'r');
Rotate by 45 degrees and translate by 1, -2
xy=[x; y; ones(size(x))]
xy =
0 2 2
0 1 0
1 1 1
theta=45*pi/180; tx=1; ty=-2; T=[cos(theta) sin(-theta) tx; sin(theta) cos(theta) ty; 0 0 1]
T =
0.7071 -0.7071 1.0000
0.7071 0.7071 -2.0000
0 0 1.0000
do transformation
xyp=T*xy
xyp =
1.0000 1.7071 2.4142
-2.0000 0.1213 -0.5858
1.0000 1.0000 1.0000
Pull out new x and y values
xp=xyp(1,:) yp=xyp(2,:)
xp = 1.0000 1.7071 2.4142 yp = -2.0000 0.1213 -0.5858
Update shape
set(myTriangle,'Xdata',xp,'Ydata',yp);