author | Santosh G. Vattam <vattam.santosh@gmail.com> |
Wed, 14 Apr 2010 16:19:25 +0530 | |
changeset 63 | 0778e21efe3c |
parent 10 | a63d7dcba725 |
permissions | -rw-r--r-- |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
1 |
* Arrays |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
2 |
*** Outline |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
3 |
***** Introduction |
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
4 |
******* What do we want to do |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
5 |
********* We shall use arrays (mentioned before) which |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
6 |
********* shall be used for matrices in future |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
7 |
******* Arsenal Required |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
8 |
********* working knowledge of lists |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
9 |
***** Initializing matrices |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
10 |
******* Entering element-wise |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
11 |
******* using special functions |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
12 |
***** Accessing and Changing elements |
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
13 |
******* Slicing?? |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
14 |
******* Striding?? |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
15 |
***** {{Simple operations??}} |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
16 |
*** Script |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
17 |
Welcome to the Tutorial on arrays. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
18 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
19 |
As mentioned in [the previous tutorial] arrays are much faster and |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
20 |
more efficient. In this tutorial we shall look at creating arrays, |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
21 |
accessing elements and changing them. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
22 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
23 |
--- |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
24 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
25 |
Let's start with creating simple arrays. We've already seen how to |
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
26 |
convert lists to arrays. Inputting a new array is similar to that. |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
27 |
|
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
28 |
In []: a = array([5, 8, 10, 13]) |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
29 |
|
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
30 |
Type /a/, to see what it is. |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
31 |
|
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
32 |
In []: a |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
33 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
34 |
We enter a multi-dimensional array this way - |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
35 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
36 |
In []: c = array([[11,12,13], |
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
37 |
[21,22,23], |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
38 |
[31,32,33]]) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
39 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
40 |
To see what c is, we just type c in the prompt. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
41 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
42 |
In []: c |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
43 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
44 |
To see the dimensions of the array c, we use c.shape |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
45 |
In []: c.shape |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
46 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
47 |
Now let us look at some special methods of creating an |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
48 |
array. There are various functions that allow us to create special |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
49 |
arrays. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
50 |
|
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
51 |
The first one we shall look at is, /arange/. /arange/ is similar to |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
52 |
the range command, except that it returns an array and accepts |
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
53 |
float arguments. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
54 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
55 |
In []: a = arange(10) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
56 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
57 |
In []: a |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
58 |
This is the array we just created. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
59 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
60 |
In []: a.shape |
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
61 |
Note that /a/ is one dimensional and has 10 elements, as expected. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
62 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
63 |
We could also use a.shape to change the shape of the array a. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
64 |
In []: a.shape = 2,5 |
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
65 |
Note that the total size of new array must be unchanged. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
66 |
|
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
67 |
We type a, to see what it looks like |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
68 |
In []: a |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
69 |
|
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
70 |
ones command can be used to get an array with all the entries as |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
71 |
ones. We pass it the shape of the array that we require. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
72 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
73 |
In []: b = ones((3, 4)) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
74 |
|
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
75 |
Look at b, by printing it out. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
76 |
In []: b |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
77 |
|
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
78 |
To create an array with all entries as ones, with it's shape |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
79 |
similar to an already existing array, we use the ones_like |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
80 |
command. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
81 |
In []: b = ones_like(a) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
82 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
83 |
zeros and zeros_like are similar commands that can give you arrays |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
84 |
with all zeros. empty and empty_like give you empty arrays (arrays |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
85 |
with no initialization done.) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
86 |
|
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
87 |
In []: b = zeros((3, 4)) |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
88 |
In []: b = zeros_like(a) |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
89 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
90 |
The identity command can be used to obtain a square array with |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
91 |
ones on the main diagonal. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
92 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
93 |
In []: identity(3) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
94 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
95 |
To obtain a 2-D array, that is not necessarily square, eye command |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
96 |
can be used. Look at the documentation of eye (using eye?) for |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
97 |
more info. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
98 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
99 |
--- |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
100 |
|
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
101 |
Now that we have learnt how to create arrays, let's move on to |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
102 |
accessing elements and changing them. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
103 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
104 |
Let's work with the c, array which we had already created. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
105 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
106 |
In []: c |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
107 |
|
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
108 |
Let's say we want to access the element 23 in c, we say |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
109 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
110 |
In []: c[1][2] |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
111 |
Note that this is similar to accessing an element inside a list of |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
112 |
lists. Also, note that counting again starts from 0. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
113 |
|
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
114 |
But arrays provide a more convenient way to access the elements. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
115 |
In []: c[1, 2] |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
116 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
117 |
Now, we can also change the element using a simple assignment. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
118 |
In []: c[1, 2] = -23 |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
119 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
120 |
Let's look at accessing more than one elements at a time. We begin |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
121 |
with accessing rows. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
122 |
In []: c[1] gives us the second row. (counting starts from 0) |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
123 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
124 |
To get a column, we use a syntax that is similar to the one used |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
125 |
to access a single element. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
126 |
In []: c[:,1], gives us the first column. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
127 |
|
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
128 |
The colon specifies that we wish to obtain all elements in that |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
129 |
dimension from the array. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
130 |
|
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
131 |
So, we could use a more explicit way to access the second row of |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
132 |
the array. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
133 |
In []: c[1,:] |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
134 |
|
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
135 |
The colon can be used to access specific portions of the array, |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
136 |
similar to the way we do with lists. |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
137 |
In []: c[1,1:3] |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
138 |
Observe that we get the second and third columns from the second |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
139 |
row. As with lists, the number after the colon is excluded when |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
140 |
slicing a portion of the array. |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
141 |
|
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
142 |
In []: c[1:3,1] |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
143 |
Now, we get the second and third rows from the first column. |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
144 |
|
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
145 |
In []: c[1:3,1:3] |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
146 |
We get the second and third rows and the second and third |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
147 |
columns. |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
148 |
|
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
149 |
The numbers before and after the colons are optional. If the |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
150 |
number before the colon is omitted, it is assumed to be zero by |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
151 |
default. If the element after the colon is omitted, it is assumed |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
152 |
to be until the end. |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
153 |
|
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
154 |
In []: c[1:, 1:] |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
155 |
This is essentially similar to the previous example. We are using |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
156 |
the default value i.e, the end, instead of specifying 3, |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
157 |
explicitly. |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
158 |
|
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
159 |
In []: c[:2, :2] |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
160 |
We have omitted specifying the zero before the colon, explicitly. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
161 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
162 |
--- |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
163 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
164 |
You may have observed the similarity of the semi-colon notation to |
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
165 |
the notation used in lists. As expected, the semi-colon notation |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
166 |
also provides a way to specify a jump. This {concept/idea} is |
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
167 |
termed as Striding. |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
168 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
169 |
To get every alternate row of c, starting from the first one, we say |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
170 |
In []: c[::2,:] |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
171 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
172 |
To get every alternate row of c, starting from the second one, we |
10
a63d7dcba725
Changed arrays.org during spoken tutorial workshop.
Puneeth Chaganti <punchagan@gmail.com>
parents:
4
diff
changeset
|
173 |
say |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
174 |
In []: c[1::2,:] |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
175 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
176 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
177 |
In []: c[:,::2] |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
178 |
In []: c[::2,::2] |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
179 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
180 |
--- |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
181 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
182 |
We come to the end of this tutorial on arrays. In this tutorial, |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
183 |
you've learnt how to create arrays and access, change elements. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
184 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
185 |
Thank you. |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
186 |
|
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
187 |
*** Notes |