2014F001/User talk:areeba14/homework 8

From CDS 130

Jump to: navigation, search

Contents

  1. Image manipulation
  2. Using fill or image
  3. Color map and images
  4. Create images
  5. Plots
  6. making polygon shapes

1. Image manipulation

  • Create a 10x10 matrix that contains any number from 1 through 10 at least once. The numbers may be in any order. Create the matrix using a double for loop.
  • Create a colormap matrix such that each number is mapped to a unique color.
  • Use the image command to plot your image.
  • Use the colorbar command to show a colorbar.
  • Describe (in words) how you could change the colormap matrix to achieve the same result.



for i = 1:10

for n = 1:10

A(i, n) = randi(10);

end

end

>> A

A =

    8     4     6     2     7     3     7     7     8     5
    1     3    10     2     9     6    10     1     5     2
   10     1     8     9     9     1     4     3     9     5
   10     2     3     2     2     9     6     6     2     9
    7     4     6     5     1     3     2     2     3     5
    1    10    10     5     5     4    10     4     2     8
    4     3     5     1     2    10    10     6     1     3
    4     9     1     1     2     7     8     7     5     6
    3     8     2     7     2     4     7     8     1    10
    8     5     5     5     4     6     6     9     8     7

>> mycolormap = [0 0 0; 0 0 1; 0 1 0; 0 1 1; 1 1 0; 1 1 1; 0.5 0.5 0.5; 1 0 1; 0.5 1 0; 0.5 0.5 0];

>> colormap(mycolormap)

>> imagesc(A)

>> colorbar

%I could change the order in the colormap matrix and switching the numbers to achieve the same results

2. Using fill or image

Write a MATLAB code that draws 8-by-8 checkerboard with red and black tiles.

M = [0 1 0 1 0 1 0 1; 1 0 1 0 1 0 1 0];

M = [M; M];

M = [M; M];

mycolormap = [1 0 0; 0 0 0];

colormap(mycolormap)

imagesc(M)

3. Color map and images

Write a Matlab script to create an image as follows: ( Note: you need to provide seven colors and the correct orientation of the strips.) Image:imagescool.jpg


mycolormap = [1 0 0; 0 0 1; 1 0 1; 0 0 0; 1 1 1; 0.5 0.5 0.5; 1 1 0]

M = [1, 2, 3, 4, 5, 6, 7]

image (M);

colormap (mcolormap) ;

axis square;

axis off;

4. Create images

Run the following Matlab code.

clear;
i=0:2*pi/1000:2*pi;
x=sin(i); 
y=cos(i); 
fill (x,y, 'r', 'EdgeColor', 'r'); 
hold on
fill (x*0.98, y*0.98, 'b', 'EdgeColor', 'b');
hold off
axis equal off
shg

Understand the meaning of this code. Now, make use of this script and write a MATLAB code to generate the Target logo.

Image:Target_logo.jpg

Your Image: Image:target.jpg


MATLAB code:

clear;

i=0:2*pi/1000:2*pi;

x=sin(i);

y=cos(i);

fill (x,y, 'r', 'EdgeColor', 'r');

hold on

fill (x*0.75, y*0.75, 'r', 'EdgeColor', 'r');

hold on

fill (x*0.50, y*0.50, 'w', 'EdgeColor', 'w');

fill (x*0.20, y*0.20, 'r', 'EdgeColor', 'r');

axis equal off

shg

5. Plots

(a) Plot the following equations, first by choosing points within the range as a vector, computing the associated y values, and finally by building a proper plot using MATLAB statements.


0 < x < 4π, y = \frac{1}{16} sin (x^4) - \frac{1}{8} cos(x^3) + \frac{1}{4} sin(x^2)-\frac{1}{2}cos(x)


Plot:


Image:thegraph.jpg

MATLAB code:

x = linspace (0, 4*pi, 100);

y =(sin(x.^4)/16)-(cos(x.^3)/8)+(sin(x.^2)/4)-(cos(x)/2);

plot(x,y)


6. making polygon shapes

Write a Matlab script to create an image as follows:


triangle


x = [0, 0, 0 ];

y = [0, 1, 1, 0 ];

z =[1, 1, 0, 0 ];

fill (x, y, z)

Personal tools