accessing-pieces-arrays/script.rst
author Amit Sethi
Thu, 11 Nov 2010 13:21:10 +0530
changeset 467 501383b753c1
parent 402 9bf3411b264d
permissions -rw-r--r--
Exercises and Slides added to Using plot interactively
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     1
.. Objectives
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     2
.. ----------
224
4e0cdb5cca53 Added questions and other info to accessing-pieces-arrays LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     3
   
239
04259d64bc14 Added pre-reqs for accessing pieces of arrays LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 224
diff changeset
     4
   .. By the end of this tutorial, you will be able to:
224
4e0cdb5cca53 Added questions and other info to accessing-pieces-arrays LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     5
   
239
04259d64bc14 Added pre-reqs for accessing pieces of arrays LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 224
diff changeset
     6
   ..   1. Access and change individual elements of arrays, both one
04259d64bc14 Added pre-reqs for accessing pieces of arrays LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 224
diff changeset
     7
   ..   dimensional and multi-dimensional.
04259d64bc14 Added pre-reqs for accessing pieces of arrays LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 224
diff changeset
     8
   ..   2. Access and change rows and columns of arrays. 
04259d64bc14 Added pre-reqs for accessing pieces of arrays LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 224
diff changeset
     9
   ..   3. Access and change other chunks from an array, using slicing
04259d64bc14 Added pre-reqs for accessing pieces of arrays LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 224
diff changeset
    10
   ..   and striding. 
04259d64bc14 Added pre-reqs for accessing pieces of arrays LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 224
diff changeset
    11
   ..   4. Read images into arrays and perform processing on them, using
04259d64bc14 Added pre-reqs for accessing pieces of arrays LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 224
diff changeset
    12
   ..   simple array manipulations. 
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    13
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    14
.. Prerequisites
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    15
.. -------------
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    16
239
04259d64bc14 Added pre-reqs for accessing pieces of arrays LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 224
diff changeset
    17
..   1. getting started with arrays
04259d64bc14 Added pre-reqs for accessing pieces of arrays LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 224
diff changeset
    18
389
edf90e5d2a79 language checked for `accessing pieces arrays`
bhanu
parents: 380
diff changeset
    19
.. #[anand: internal reviewer not mentioned]     
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    20
.. Author              : Puneeth
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    21
   Internal Reviewer   : 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    22
   External Reviewer   :
389
edf90e5d2a79 language checked for `accessing pieces arrays`
bhanu
parents: 380
diff changeset
    23
   Language Reviewer   : Bhanukiran
390
69f285b3a5ed checklist OK for `accessing pieces arrays`
anand
parents: 389
diff changeset
    24
   Checklist OK?       : <06-11-2010, Anand,  OK> [2010-10-05]
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    25
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    26
Script
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    27
------
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    28
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    29
{{{ Screen shows welcome slide }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    30
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    31
Welcome to the tutorial on accessing pieces of arrays
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
{{{ Show the outline for this tutorial }}} 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    34
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
In this tutorial we shall learn to access individual elements of
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    36
arrays, get rows and columns and other chunks of arrays using
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    37
slicing and striding. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
{{{ switch back to the terminal }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    40
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    41
As usual, we start IPython, using 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    42
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    44
  ipython -pylab 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
    46
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
    47
{{ Show the slide with the arrays, A and C }}
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
    48
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
Let us have two arrays, A and C, as the sample arrays that we will
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    50
use to work through this tutorial. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    51
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    52
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
  A = array([12, 23, 34, 45, 56])
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    55
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    56
  C = array([[11, 12, 13, 14, 15],
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    57
             [21, 22, 23, 24, 25],
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    58
             [31, 32, 33, 34, 35],
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
             [41, 42, 43, 44, 45],
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    60
             [51, 52, 53, 54, 55]])
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    61
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    62
Pause the video here and make sure you have the arrays A and C,
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    63
typed in correctly.
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    64
400
2259bd43446a Made some changes to accessing pieces of arrays while doing a test recording.
Amit Sethi
parents: 377
diff changeset
    65
{{{ Pause the recording and type the arrays A,C }}}
2259bd43446a Made some changes to accessing pieces of arrays while doing a test recording.
Amit Sethi
parents: 377
diff changeset
    66
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    67
Let us begin with the most elementary thing, accessing individual
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    68
elements. Also, let us first do it with the one-dimensional array
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    69
A, and then do the same thing with the two-dimensional array. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    70
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    71
To access, the element 34 in A, we say, 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    72
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    73
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    74
348
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
    75
  A[2]
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
    76
400
2259bd43446a Made some changes to accessing pieces of arrays while doing a test recording.
Amit Sethi
parents: 377
diff changeset
    77
A of 2, note that we are using square brackets.
2259bd43446a Made some changes to accessing pieces of arrays while doing a test recording.
Amit Sethi
parents: 377
diff changeset
    78
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    79
Like lists, indexing starts from 0 in arrays, too. So, 34, the
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    80
third element has the index 2. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    81
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    82
Now, let us access the element 34 from C. To do this, we say
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    83
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    84
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    85
  C[2, 3]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    86
400
2259bd43446a Made some changes to accessing pieces of arrays while doing a test recording.
Amit Sethi
parents: 377
diff changeset
    87
C of 2,3.
2259bd43446a Made some changes to accessing pieces of arrays while doing a test recording.
Amit Sethi
parents: 377
diff changeset
    88
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    89
34 is in the third row and the fourth column, and since indexing
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    90
begins from zero, the row index is 2 and column index is 3. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    91
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    92
Now, that we have accessed one element of the array, let us change
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    93
it. We shall change the 34 to -34 in both A and C. To do this, we
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    94
simply assign the new value after accessing the element. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    95
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    96
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    97
  A[2] = -34
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    98
  C[2, 3] = -34
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    99
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   100
Now that we have accessed and changed a single element, let us
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   101
access and change more than one element at a time; first rows and
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
then columns.
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   103
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   104
Let us access one row of C, say the third row. We do it by saying, 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   105
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   106
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   107
  C[2] 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   108
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   109
How do we access the last row of C? We could say,
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   110
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   111
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   112
  C[4] 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   113
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   114
for the fifth row, or as with lists, use negative indexing and say
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   115
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   116
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   117
  C[-1]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   118
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   119
Now, we could change the last row into all zeros, using either 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   120
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   121
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   122
  C[-1] = [0, 0, 0, 0, 0]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   123
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   124
or 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   125
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   126
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   127
  
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   128
  C[-1] = 0
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   129
380
c17aa604468a Changes to accessing parts of arrays during recording.
Puneeth Chaganti <punchagan@fossee.in>
parents: 377
diff changeset
   130
Now, how do we access one column of C? As with accessing individual
c17aa604468a Changes to accessing parts of arrays during recording.
Puneeth Chaganti <punchagan@fossee.in>
parents: 377
diff changeset
   131
elements, the column is the second parameter to be specified (after
c17aa604468a Changes to accessing parts of arrays during recording.
Puneeth Chaganti <punchagan@fossee.in>
parents: 377
diff changeset
   132
the comma). The first parameter, is replaced with a ``:``. This
c17aa604468a Changes to accessing parts of arrays during recording.
Puneeth Chaganti <punchagan@fossee.in>
parents: 377
diff changeset
   133
specifies that we want all the elements of that dimension, instead of
c17aa604468a Changes to accessing parts of arrays during recording.
Puneeth Chaganti <punchagan@fossee.in>
parents: 377
diff changeset
   134
just one particular element. We access the third column by
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   135
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   136
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   137
  
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   138
  C[:, 2]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   140
Following is an exercise that you must do. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   141
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   142
{{ show slide containing Question 1}} 
348
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
   143
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   144
%%1%% Change the last column of C to zeroes. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   145
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   146
Please, pause the video here. Do the exercises and then continue. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   147
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   148
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   149
  
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   150
  C[:, -1] = 0
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   151
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   152
Since A is one dimensional, rows and columns of A don't make much
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   153
sense. It has just one row and 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   154
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   155
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   156
  A[:] 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   157
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   158
gives the whole of A. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   159
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   160
Following is an exercise that you must do. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   161
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   162
{{ show slide containing Question 2}} 
348
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
   163
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   164
%%2%% Change ``A`` to ``[11, 12, 13, 14, 15]``. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   165
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   166
Please, pause the video here. Do the exercises and then continue. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   167
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   168
To change A, we say
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   169
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   170
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
  A[:] = [11, 12, 13, 14, 15]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   173
Now, that we know how to access, rows and columns of an array, we
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
shall learn how to access other pieces of an array. For this
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   175
purpose, we will be using image arrays. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   176
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   177
To read an image into an array, we use the ``imread`` command. We
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   178
shall use the image ``squares.png`` present in ``/home/fossee``. We
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   179
shall first navigate to that path in the OS and see what the image
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   180
contains. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   181
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   182
{{{ switch to the browser and show the image }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   183
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   184
{{{ switch back to the ipython terminal }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   185
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   186
Let us now read the data in ``squares.png`` into the array ``I``. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   187
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   188
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   189
  I = imread('/home/fossee/squares.png')
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   190
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   191
We can see the contents of the image, using the command
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   192
``imshow``. We say, 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   193
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   194
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   195
  imshow(I) 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   196
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   197
to see what has been read into ``I``. We do not see white and black
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   198
because, ``pylab`` has mapped white and black to different
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   199
colors. This can be changed by using a different colormap. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   200
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   201
To see that ``I`` is really, just an array, we say, 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   202
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   203
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   204
  I 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   205
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   206
at the prompt, and see that an array is displayed. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   207
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   208
To check the dimensions of any array, we can use ``.shape``. We say
348
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
   209
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   210
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   211
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   212
  I.shape 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   213
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   214
to get the dimensions of the image. As we can see, ``squares.png``
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   215
has the dimensions of 300x300. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   216
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   217
Our goal for this part of the tutorial would be to get the
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   218
top-left quadrant of the image. To do this, we need to access, a
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   219
few of the rows and a few of the columns of the array. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   220
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   221
To access, the third column of C, we said, ``C[:, 2]``. Essentially,
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   222
we are accessing all the rows in column three of C. Now, let us
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   223
modify this to access only the first three rows, of column three
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   224
of C. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   225
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   226
We say, 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   227
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   228
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   229
  C[0:3, 2]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   230
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   231
to get the elements of rows indexed from 0 to 3, 3 not included
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   232
and column indexed 2. Note that, the index before the colon is
389
edf90e5d2a79 language checked for `accessing pieces arrays`
bhanu
parents: 380
diff changeset
   233
included and the index after it is not included in the slice that
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   234
we have obtained. This is very similar to the ``range`` function,
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   235
where ``range`` returns a list, in which the upper limit or stop
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   236
value is not included.
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   237
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   238
Now, if we wish to access the elements of row with index 2, and in
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   239
columns indexed 0 to 2 (included), we say, 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   240
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   241
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   242
  C[2, 0:3]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   243
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   244
Following is an exercise that you must do. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   245
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   246
{{ show slide containing Question 3 }} 
348
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
   247
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   248
%%3%% First, obtain the elements [22, 23] from C. Then, obtain the
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   249
elements [11, 21, 31, 41] from C. Finally, obtain the elements [21,
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   250
31, 41, 0]. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   251
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   252
Please, pause the video here. Do the exercises and then continue. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   253
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   254
{{ show slide containing Solution 3 }}
348
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
   255
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   256
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   257
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   258
  C[1, 1:3] 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   259
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   260
gives the elements [22, 23]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   261
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   262
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   263
  C[0:4, 0]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   264
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   265
gives the elements [11, 21, 31, 41]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   266
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   267
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   268
  C[1:5, 0]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   269
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   270
gives the elements [21, 31, 41, 0]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   271
380
c17aa604468a Changes to accessing parts of arrays during recording.
Puneeth Chaganti <punchagan@fossee.in>
parents: 377
diff changeset
   272
Note that when specifying ranges, if you are starting from the
c17aa604468a Changes to accessing parts of arrays during recording.
Puneeth Chaganti <punchagan@fossee.in>
parents: 377
diff changeset
   273
beginning or going up-to the end, the corresponding element may be
c17aa604468a Changes to accessing parts of arrays during recording.
Puneeth Chaganti <punchagan@fossee.in>
parents: 377
diff changeset
   274
dropped. So, in the previous example to obtain [11, 21, 31, 41], we
c17aa604468a Changes to accessing parts of arrays during recording.
Puneeth Chaganti <punchagan@fossee.in>
parents: 377
diff changeset
   275
could have simply said, ::
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   276
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   277
  C[:4, 0]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   278
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   279
and 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   280
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   281
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   282
  C[1:, 0]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   283
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   284
gives the elements [21, 31, 41, 0]. If we skip both the indexes,
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   285
we get the slice from end to end, as we already know. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   286
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   287
Following is an exercise that you must do. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   288
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   289
{{ show slide containing Question 4 }} 
348
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
   290
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   291
%%4%% Obtain the elements [[23, 24], [33, -34]] from C. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   292
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   293
Please, pause the video here. Do the exercises and then continue. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   294
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   295
{{ show slide containing Solution 4 }} 
348
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
   296
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   297
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   298
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   299
  C[1:3, 2:4] 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   300
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   301
gives us the elements, [[23, 24], [33, -34]]. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   302
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   303
Now, we wish to obtain the top left quarter of the image. How do
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   304
we go about doing it? Since, we know the shape of the image to be
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   305
300, we know that we need to get the first 150 rows and first 150
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   306
columns. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   307
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   308
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   309
  I[:150, :150]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   310
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   311
gives us the top-left corner of the image. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   312
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   313
We use the ``imshow`` command to see the slice we obtained in the
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   314
form of an image and confirm. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   315
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   316
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   317
  imshow(I[:150, :150])
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   318
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   319
Following is an exercise that you must do. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   320
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   321
{{ show slide containing Question 5 }} 
348
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
   322
270
49c9bb72e08a Slides for accessing parts of arrays LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 239
diff changeset
   323
%%5%% Obtain the square in the center of the image.
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   324
380
c17aa604468a Changes to accessing parts of arrays during recording.
Puneeth Chaganti <punchagan@fossee.in>
parents: 377
diff changeset
   325
Please, pause the video here. Do the exercises and then continue. 
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   326
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   327
{{ show slide containing Solution 5 }} 
348
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
   328
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   329
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   330
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   331
  imshow(I[75:225, 75:225])
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   332
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   333
Our next goal is to compress the image, using a very simple
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   334
technique to reduce the space that the image takes on disk while
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   335
not compromising too heavily on the image quality. The idea is to
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   336
drop alternate rows and columns of the image and save it. This way
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   337
we will be reducing the data to a fourth of the original data but
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   338
losing only so much of visual information. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   339
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   340
We shall first learn the idea of striding using the smaller array
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   341
C. Suppose we wish to access only the odd rows and columns (first,
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   342
third, fifth). We do this by, 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   343
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   344
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   345
  C[0:5:2, 0:5:2]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   346
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   347
if we wish to be explicit, or simply, 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   348
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   349
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   350
  C[::2, ::2]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   351
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   352
This is very similar to the step specified to the ``range``
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   353
function. It specifies, the jump or step in which to move, while
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   354
accessing the elements. If no step is specified, a default value
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   355
of 1 is assumed. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   356
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   357
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   358
  C[1::2, ::2] 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   359
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   360
gives the elements, [[21, 23, 0], [41, 43, 0]]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   361
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   362
{{ show slide containing Question 6 }} 
348
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
   363
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   364
Following is an exercise that you must do. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   365
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   366
%%6%% Obtain the following. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   367
[[12, 0], [42, 0]]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   368
[[12, 13, 14], [0, 0, 0]]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   369
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   370
Please, pause the video here. Do the exercises and then continue. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   371
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   372
{{ show slide containing Solution 6 }} 
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   373
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   374
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   375
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   376
  C[::3, 1::3]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   377
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   378
gives the elements [[12, 0], [42, 0]]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   379
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   380
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   381
  C[::4, 1:4]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   382
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   383
gives the elements [[12, 13, 14], [0, 0, 0]]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   384
380
c17aa604468a Changes to accessing parts of arrays during recording.
Puneeth Chaganti <punchagan@fossee.in>
parents: 377
diff changeset
   385
Now, that we know how to stride over an array, we can drop
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   386
alternate rows and columns out of the image in I. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   387
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   388
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   389
  I[::2, ::2]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   390
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   391
To see this image, we say, 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   392
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   393
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   394
  imshow(I[::2, ::2])
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   395
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   396
This does not have much data to notice any real difference, but
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   397
notice that the scale has reduced to show that we have dropped
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   398
alternate rows and columns. If you notice carefully, you will be
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   399
able to observe some blurring near the edges. To notice this
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   400
effect more clearly, increase the step to 4. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   401
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   402
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   403
  imshow(I[::4, ::4])
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   404
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   405
{{{ show summary slide }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   406
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   407
That brings us to the end of this tutorial. In this tutorial, we
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   408
have learnt to access parts of arrays, specifically individual
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   409
elements, rows and columns and larger pieces of arrays. We have
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   410
also learnt how to modify arrays, element wise or in larger
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   411
pieces.
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   412
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   413
{{{ Show the "sponsored by FOSSEE" slide }}}
348
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
   414
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   415
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
348
3f70aeb1e115 reviewed accessing parts of array script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 339
diff changeset
   416
377
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   417
Hope you have enjoyed and found it useful.
17f08d039309 Changes to accessing pieces of arrays, based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents: 348
diff changeset
   418
Thank you!
339
09476318db60 Changes to progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents: 270
diff changeset
   419
09476318db60 Changes to progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents: 270
diff changeset
   420
.. 
09476318db60 Changes to progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents: 270
diff changeset
   421
   Local Variables:
09476318db60 Changes to progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents: 270
diff changeset
   422
   mode: rst
09476318db60 Changes to progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents: 270
diff changeset
   423
   indent-tabs-mode: nil
09476318db60 Changes to progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents: 270
diff changeset
   424
   sentence-end-double-space: nil
380
c17aa604468a Changes to accessing parts of arrays during recording.
Puneeth Chaganti <punchagan@fossee.in>
parents: 377
diff changeset
   425
   fill-column: 70
339
09476318db60 Changes to progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents: 270
diff changeset
   426
   End: