%Psych 583B
%Homework 2
%Geoff Boynton
%
%Part 1:
%
%Create the following matrix M:
%
%   [ 2     2     2     2     2     2
%     1     2     2     2     2     2
%     1     1     2     2     2     2
%     1     1     1     2     2     2
%     1     1     1     1     2     2
%     1     1     1     1     1     2]
%
%by using a nested for loop where the variable i loops through the rows and j
%loops through the columns.  Inside the loop, use an if statement so that
%if the row value is greater then the column value (i.e. i>j) set that
%element of the matrix to 1, otherwise set it to 2.


M = zeros(6);

for i=1:6
    for j=1:6
        if i>j
            M(i,j) = 1;
        else
            M(i,j) = 2;
        end
    end
end

%% Part 2
%
%Create a color map containing two colors, red and green.  Use the 'image'
%commmand and colormap command to show the matrix M that you made above
%using the color map.  The image should look like this:

cmap = [1,0,0;0,1,0];

figure(1)
image(M)
colormap(cmap);


%% Part 3
%
%Create a new matrix N so that when you show it using the image command,
%using the same red green color map from part 2, the image looks like
%the image below.  You can use any method you want to make the matrix N - a nested
%for loop, or by just defining it by hand.

% doing it by hand:
N = [1,2,1,2,1,2
    2,1,2,1,2,1
    1,2,1,2,1,2
    2,1,2,1,2,1
    1,2,1,2,1,2
    2,1,2,1,2,1];

% as a nested loop
n = 6;  %size of matrix
N = ones(6);
for i=1:n
    for j=1:n
        if mod(i+j,2)
            N(i,j) = 2;
        end
    end
end

%Or, now that you know about meshgrid we can do it in two lines!

[i,j] = meshgrid(1:n);
N = mod(i+j,2)+1;


figure(2)
image(N)
colormap(cmap);
