# HG changeset patch # User Puneeth Chaganti # Date 1286371597 -19800 # Node ID 4e0cdb5cca53cca7ccd4ba1113da790c991340c7 # Parent ce0e8c99eaee951a572d331441067607d9dd9213 Added questions and other info to accessing-pieces-arrays LO. diff -r ce0e8c99eaee -r 4e0cdb5cca53 accessing-pieces-arrays/questions.rst --- a/accessing-pieces-arrays/questions.rst Wed Oct 06 17:31:20 2010 +0530 +++ b/accessing-pieces-arrays/questions.rst Wed Oct 06 18:56:37 2010 +0530 @@ -1,17 +1,159 @@ -Objective ---------- +Objective Questions +------------------- + +.. A mininum of 8 questions here (along with answers) + +1. Given the array, ``A = array([12, 15, 18, 21])``, how do we access + the element ``18``? + + Answer: ``A[2]`` + +#. Given the array, ``A = array([12, 15, 18, 21])``, what element is + accessed by ``A[1]`` + + Answer: ``15`` + +#. Given the array, ``A = array([12, 15, 18, 21])``, what element is + accessed by ``A[-3]``? + + Answer: ``15`` + +#. Given the array, ``A = array([12, 15, 18, 21])``, how do you change + the element ``15`` to ``51``? + + Answer: ``A[1] = 51`` -.. A mininum of 8 questions here. +#. Given the array, ``A = array([12, 15, 18, 21])``, what is ``A`` + after the execution of the statement ``A[3] = 24``? + + Answer: ``array([12, 15, 18, 24])`` + +#. Given the array, ``A = array([12, 15, 18, 21])``, what is the + result of the statement, ``A[4] = 24``? + + Answer: IndexError: index out of bounds + +#. Given the array, ``B = array([[12, 15], [18, 21]])``, how do we + access the element ``18``? + + Answer: ``A[0, 0]`` + +#. Given the array, ``B = array([[12, 15], [18, 21]])``, what is the + result of ``B[1]``? + + Answer: ``array([18, 21])`` + +#. Given the array, ``B = array([[12, 15], [18, 21]])``, what is the + result of ``B[1, 1]``? + + Answer: ``21`` + +#. Given the array, ``B = array([[12, 15], [18, 21]])``, what element + is accessed by ``B[-1, -2]``? -1. Question 1 -2. Question 2 -3. Question 3 + Answer: ``18`` + +#. Given the array, ``B = array([[12, 15], [18, 21]])``, how do you + change the element ``15`` to ``51``? + + Answer: ``B[0, 1] = 51`` + +#. Given the array, ``B = array([[12, 15], [18, 21]])``, what is the + value of B, after executing, ``B[0] = 0``? + + Answer: ``array([[0, 0], [18, 21]])`` + +#. Given the array, + + :: + + B = array([[10, 11, 12, 13], + [20, 21, 22, 23], + [30, 31, 32, 33], + [40, 41, 42, 43]]) + + Obtain the elements, ``[[21, 22], [32, 32]]`` + + Answer: ``B[1:3, 1:3]`` +#. Given the array, + + :: + + B = array([[10, 11, 12, 13], + [20, 21, 22, 23], + [30, 31, 32, 33], + [40, 41, 42, 43]]) + Obtain the elements, ``[[10, 13], [30, 33]]`` + + Answer: ``B[::2, ::3]`` + +#. What command is used to read an image file? + + a. imshow + #. imread + #. imopen + #. open + + Answer: ``imread`` + +#. If ``P`` is ``array([12, 13, 14, 15])`` then ``P.shape`` gives + ``(4, 1)``. True or False? + + Answer: False + Larger Questions ---------------- -.. A minimum of 2 questions here. +.. A minimum of 2 questions here (along with answers) + +1. Given the array, + :: + + B = array([[10, 11, 12, 13], + [20, 21, 22, 23], + [30, 31, 32, 33], + [40, 41, 42, 43]]) + + Change the array to + :: + + B = array([[10, 11, 10, 11], + [20, 21, 20, 21], + [10, 11, 10, 11], + [20, 21, 20, 21]]) + + using slicing and striding. + + Answer:: + + B[:2, 2:] = B[:2, :2] + B[2:, :2] = B[:2, :2] + B[2:, 2:] = B[:2, :2] -1. Programming Assignment 1 -2. Programming Assignment 2 +#. Find out how to display a grayscale image with proper colors. Hint, + use ``?`` on ``imshow``. + + Answer: ``cmap=cm.gray`` + +#. From the image, ``tagore-einstein.png``, + a. show only the face of Einstein. + b. show only the face of Tagore. + + Hint, use ``imread`` to read the image and ``imshow`` to show the + image. Use ``cm.gray`` as ``cmap`` to display the image properly. + + Answer:: + + F = imread('tagore-einstein.png') + imshow(F[:210, 20:180], cmap=cm.gray) + + F = imread('tagore-einstein.png') + imshow(F[30:300, 320:480], cmap=cm.gray) + +.. #[Puneeth: This question contains answer to previous +.. question. Shouldn't be shown at the same time.] + + + diff -r ce0e8c99eaee -r 4e0cdb5cca53 accessing-pieces-arrays/script.rst --- a/accessing-pieces-arrays/script.rst Wed Oct 06 17:31:20 2010 +0530 +++ b/accessing-pieces-arrays/script.rst Wed Oct 06 18:56:37 2010 +0530 @@ -1,7 +1,15 @@ .. Objectives .. ---------- - -.. Clearly state the objectives of the LO (along with RBT level) + + By the end of this tutorial, you will be able to: + + 1. Access and change individual elements of arrays, both one + dimensional and multi-dimensional. + 2. Access and change rows and columns of arrays. + 3. Access and change other chunks from an array, using slicing + and striding. + 4. Read images into arrays and perform processing on them, using + simple array manipulations. .. Prerequisites .. ------------- @@ -18,7 +26,6 @@ Script ------ - {{{ Screen shows welcome slide }}} Welcome to the tutorial on accessing pieces of arrays diff -r ce0e8c99eaee -r 4e0cdb5cca53 accessing-pieces-arrays/tagore-einstein.png Binary file accessing-pieces-arrays/tagore-einstein.png has changed