%% E5 MatLab - 2nd class demo %% % 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) %% % A matrix of zeros (by default, square) Z=zeros(3) %% % A non-square matrix of zeros Z=zeros(2,5) %% % Can also create a matrix of ones... O=ones(3) %% % We can make the matrix the same size as another array O=ones(size(Z)) %% % Define a matrix of random numbers R=rand(2,5) %% Working with arrays and subsets of arrays A=[1 -2 3 -4; -2 3 -4 5; -2 -4 -6 -8] %% % find size size(A) %% % find number of rows (the first dimension of the A matrix) size(A,1) %% % what is the 2nd row? A(2,:) %% % what is the 3rd column? A(:,3) %% % Define an array of zeros same size as a row of A (1 row, same number of columns) z=zeros(1, size(A,2)) %% % now change the last row A(3,:)=z %% Higher dimensional arrays X=rand(2,4,3) %% % Take "top" array X(:,:,1) %% % Take 3rd column of "middle" array X(:,3,2) %% % Take first two elements of 2nd row of "bottom" array X(2,1:2,3) %% Using a function % p=3 % Define a variable "p" for no real reason. %% % 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) %% % Note that the variable p is unchanged p %% 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] %% % Perform rotation and plot % Note, for the transformations we add a third row that is always 1 xy=[x; y; 1] %% xyp=T*xy %% xp=xyp(1) yp=xyp(2) %% % 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] %% % do transformation xyp=T*xy xp=xyp(1); yp=xyp(2); %% % 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] %% % do transformation xyp=T*xy xp=xyp(1); yp=xyp(2); %% % 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))] %% theta=45*pi/180; tx=1; ty=-2; T=[cos(theta) sin(-theta) tx; sin(theta) cos(theta) ty; 0 0 1] %% % do transformation xyp=T*xy %% % Pull out new x and y values xp=xyp(1,:) yp=xyp(2,:) %% % Update shape set(myTriangle,'Xdata',xp,'Ydata',yp);