Thursday, May 22, 2008

Assignment 4 - as good as it's going to get

On Octave/Matlab here are the commands (obviously these would need to be done somewhat sequentially since the ones below reference those above ... saves on typing). Note that these first few were done in the Gauss lab (using Octave) and the later ones were done using Matlab 2007, so the commands might be slightly different:

(1) make a matrix of 256 x 1 ones: ones(256,1)
(2) make a matrix of the integers 0 through 255 in a single row: A=[0:1:255]
(3) make a column of the integers 0 through 255: A'
(4) make a 256 x 256 matrix with the rows all the same equal to the numbers 0 to 255: B=ones(256,1)*A
(5) make a 256 x 256 matrix with the columns all the same equal to the numbers 0 to 255: B'
(6) make a 256 x 256 matrix of all zeros: zeros(256)
(7) make a 256 x 256 matrix of all ones: ones(256)
(8) make a 256 x 256 matrix of the value 128: 128*ones(256)
(9) make a 256 x 256 matrix of all zeros - it's still just: zeros(256)
(10) display a greyscale image like the following: imshow(B)










(11) display the RB face of the colour cube:


RB(:,:,1)=[ones(256,1)*[0:1:255]/255]';
RB(:,:,2)=zeros(256);
RB(:,:,3)=[ones(256,1)*[0:1:255]/255];
imshow(RB)





(12) display the RG face of the colour cube:


RG(:,:,1)=[ones(256,1)*[0:1:255]/255]';
RG(:,:,2)=[ones(256,1)*[0:1:255]/255];
RG(:,:,3)=zeros(256);
imshow(RG)




(13) display the GB face of the colour cube:



GB(:,:,1)=zeros(256);
GB(:,:,2)=[ones(256,1)*[0:1:255]/255]';
GB(:,:,3)=[ones(256,1)*[255:-1:0]/255];
imshow(GB)





(14) display the CM face of the colour cube:


CM(:,:,1)=[ones(256,1)*[0:1:255]/255]';
CM(:,:,2)=[ones(256,1)*[0:1:255]/255];
CM(:,:,3)=ones(256);
imshow(CM)





(15) display the CY face of the colour cube:



CY(:,:,1)=[ones(256,1)*[0:1:255]/255];
CY(:,:,2)=ones(256);
CY(:,:,3)=[ones(256,1)*[0:1:255]/255]';
imshow(CY)



(16) display the YM face of the colour cube:


YM(:,:,1)=ones(256);
YM(:,:,2)=[ones(256,1)*[0:1:255]/255]';
YM(:,:,3)=[ones(256,1)*[0:1:255]/255];
imshow(YM)





(17) display the whole colour cube in a cross with white regions where there doesn't need to be anything:















>> W(:,:,1)=ones(256);
>> W(:,:,2)=ones(256);
>> W(:,:,3)=ones(256);
>> RG(:,:,1)=[ones(256,1)*[0:1:255]/255];
>> RG(:,:,2)=[ones(256,1)*[0:1:255]/255]';
>> RG(:,:,3)=zeros(256);
>> RG=imrotate(RG,90);
>> GB(:,:,1)=zeros(256);
>> GB(:,:,2)=[ones(256,1)*[0:1:255]/255];
>> GB(:,:,3)=[ones(256,1)*[0:1:255]/255]';
>> GB=imrotate(GB,90);
>> CM(:,:,1)=[ones(256,1)*[0:1:255]/255]';
>> CM(:,:,2)=[ones(256,1)*[0:1:255]/255];
>> CM(:,:,3)=ones(256);
>> CM=imrotate(CM,180);
>> CY(:,:,1)=[ones(256,1)*[0:1:255]/255];
>> CY(:,:,2)=ones(256);
>> CY(:,:,3)=[ones(256,1)*[0:1:255]/255]';
>> CY=imrotate(CY,180);
>> YM(:,:,1)=ones(256);
>> YM(:,:,2)=[ones(256,1)*[0:1:255]/255]';
>> YM(:,:,3)=[ones(256,1)*[0:1:255]/255];
>> RB(:,:,1)=[ones(256,1)*[0:1:255]/255]';
>> RB(:,:,2)=zeros(256);
>> RB(:,:,3)=[ones(256,1)*[0:1:255]/255];
>> imshow([W,RB,W;W,YM,W;RG,CY,CM;W,GB,W])


(18) display something similar to the rainbow image that appears on the Math 5300 facebook page:

In the rainbow image from Facebook, only the following colours appear: red, orange, yellow, green, blue, violet. So I tried to create an image without cyan or magenta.







The commands to create this image were:
R(:,:,1)=ones(256);
R(:,:,2)=[ones(256,1)*[0:1:255]/255];
R(:,:,3)=zeros(256);
Y(:,:,1)=[ones(256,1)*[255:-1:0]/255];
Y(:,:,2)=ones(256);
Y(:,:,3)=zeros(256);
G(:,:,1)=zeros(256);
G(:,:,2)=ones(256,1)*[0:1:255]/255;
G(:,:,3)=ones(256,1)*[255:-1:0]/255;
GB=imrotate(G,180);
B(:,:,1)=ones(256,1)*[0:1:255]/255;
B(:,:,2)=zeros(256);
B(:,:,3)=ones(256);
I=[R,Y,GB,B];
imshow(I)


(19) shift the entries of a matrix A one place left (wrapping around left -> right):

I wrote the detailed explanation in 21) below for how the diag(x, n) command works. In order to shift the entries of a matrix A one place left (with wrap around), here's what you need to do, after defining the nxn matrix A:
Create an nxn identity matrix: x=ones(n, 1);
Shift the main diagonal down 1: B=diag(x(1:(n-1)),-1);
Replace the nth column: B(:,n)=[1 0 0 0 ... 0]; (there are n-1 zeros)
To shift the entries of A: A*B

(20) shift the entries of a matrix A one place down (wrapping around bottom -> top):
After defining the matrix A, use the same command sequence as for 19) above, but in the last step multiply B*A.

(21) shift the entries of a matrix A one place left (dropping values on the left edge):

In order to do this, you need to multiply the matrix A on the right side by a matrix consisting of 1's on the 1st sub-diagonal and 0's everywhere else.

The Matlab/Octave command to create this matrix is: diag(x, n) where the nth diagonal will contain the vector x. If n is 0, the vector will go on the main diagonal; if n is positive the vector will go on the nth super-diagonal and if n is negative the vector will go on the nth sub-diagonal. Note that the nth diagonal is shorter by n than the leading diagonal, so for instance if x=ones(3,1) then diag(x,0) produces a 3x3 matrix, while diag(x,1) and diag(x,-1) produce 4x4 matrices.
So, for our particular question, we would first need to define the nxn matrix A and then define a matrix x=ones(n,1). If we set B=diag(x(1:(n-1)),-1) and then multiply A*B we shift the entries of A one place left.

(22) shift the entries of a matrix A one place down (dropping values on the bottom edge):
The explanation and setup is the same as for 21) above, but this time multiply B*A.

[Incidentally to shift the entries of a matrix A one place right, you would define x=ones(n,1) and then B=diag(x(1:(n-1)),1) and then multiply A*B. To shift the entries of a matrix A one place up, set up x and B like this and multiply B*A.]

No comments: