% LAB_15: 2016-07-19
% EXCEL EA-1
% Mini-tutorial: Visualizing matrices
clear
clc
close all
format compact
%% 1: Display the matrix as an image
close all
% Create a random matrix of ones and zeros
M = rand(30) > 0.2;
M(1:10, 1:10) = 0; % Put a black box in the top-left corner
% Display the matrix as an image, where each entry correpsonds to a single pixel
image(M)
% Use a grayscale colormap with 2 values (i.e. black and white)
colormap(gray(2))
% This command makes the x scale and y scale equal, so that each matrix
% element is displayed as a square, rather than a rectangle
axis equal
%% 2: Create a black and white checkerboard plot
close all
% Create a random matrix of ones and zeros
M = rand(30) > 0.2;
M(1:10, 1:10) = 0; % Put a black box in the top-left corner
% The pcolor command creates a checkerboard plot where the color of each
% square is given by the value in the matrix.
%
%!!!!!! NOTE: THE PCOLOR COMMAND DOES NOT PLOT THE LAST ROW OR COLUMN OF
% THE MATRIX, SO YOU NEED TO 'PAD' THE MATRIX WITH AN EXTRA ROW AND
% COLUMN OF ZEROS.
pcolor( padarray(M, [1, 1], 'post') ) % Draw the padded array
colormap(gray(2)) % Create a color map with only black and white
shading flat % Remove the lines between squares
% By default, the first row is at the bottom and the rows stack upwards.
% This command switches the order so that the first row is at the top.
axis ij
% This command makes the x scale and y scale equal, so that each matrix
% element is displayed as a square, rather than a rectangle
axis equal
%% 3: Create a "dot plot" where each matrix element is a single marker
close all
% Create a random matrix of ones and zeros
M = rand(30) > 0.2;
M(1:10, 1:10) = 0; % Put a black box in the top-left corner
% For every element in the matrix M, plot a circle if its a 1, and a dot if
% its a 0
hold on
for i = 1:size(M, 1)
for j = 1:size(M, 2)
if M(i, j) % Check if M(i, j) is a 1
plot(i, j, 'ro') % Plot a red circle at the point i, j
else
plot(i, j, 'kx') % Plot a black dot at the point i, j
end
end
end
% By default, the first row is at the bottom and the rows stack upwards.
% This command switches the order so that the first row is at the top.
axis ij
% This command makes the x scale and y scale equal, so that each matrix
% element is displayed as a square, rather than a rectangle
axis equal