accessing-pieces-arrays/questions.rst
author amit
Wed, 13 Oct 2010 17:28:04 +0530
changeset 322 3cacbcad4c42
parent 224 4e0cdb5cca53
permissions -rw-r--r--
Adding plotting data

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``

#. 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]``?

   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 (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]

#. 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.]