arrays.txt
changeset 55 1fe734b20950
parent 54 46a3575919f5
child 56 86c862b3dbef
equal deleted inserted replaced
54:46a3575919f5 55:1fe734b20950
    30 i = identity(3)
    30 i = identity(3)
    31 i
    31 i
    32 i = identity(5)
    32 i = identity(5)
    33 i
    33 i
    34 
    34 
       
    35 ----------------
       
    36 Now that we have covered creation of arrays, we will see how to access and change values of particular elements. 
       
    37 We created one 3x3 matrix earlier, 
       
    38 c
       
    39 
       
    40 to access the element 23 we type
       
    41 c[1][2]
       
    42 
       
    43 It is second row third column of c. Note that index values of arrays also start from 0.
       
    44 Alternative and popular way of doing this is
       
    45 c[1, 2]
       
    46 
       
    47 here ',' is used as separator for row and column value. Similarly any value from the array can be accessed.
       
    48 
       
    49 to access particular row completely we simply skip the column value
       
    50 c[1]
       
    51 will give us the entire second row.
       
    52 
       
    53 The way by which we access one element of array, we use that itself to assign new value
       
    54 c[1, 1] = -22
       
    55 c
       
    56 
       
    57 and same thing can be used for entire row
       
    58 c[1] = 0
       
    59 c
       
    60 as we can see, second row all elements are now 0
       
    61 
       
    62 Accessing a row is straight forward we skip column part
       
    63 but to access whole column we have to use
       
    64 c[:,2]
       
    65 will return third column.
       
    66 here the ':' part mentioned for row value symbolises entire row.
       
    67 the c[1] we were using earlier can also be written as c[1,:]
       
    68 
       
    69 ':' actually takes two value. for any row or column we can mention
       
    70 start:end values, and rows/columns starting for 'start' till 'end' will be returned. Lets try some examples for better understanding
       
    71 c[0:2,:]
       
    72 will result in rows starting from first(0) till second and all columns. Note here that 'end' in our case, '2' would not be included in resulting array.
       
    73 
       
    74 c[1:3,:] 
       
    75 gives second and third row.
       
    76 
       
    77 similarly we can try this on columns also:
       
    78 c[:, 0:2] gives us first two column
       
    79 This whole concept of accessing chunks of arrays is known as 'slicing'
       
    80 
       
    81 There is one more interesting and handy feature of slicing. We saw earlier that how only ':' means entire row or column.
       
    82 It actually means if we don't specify start and end part of slice default is from zero to end.
       
    83 
       
    84 so
       
    85 c[:, :2]
       
    86 will also give us first two columns
       
    87 and c[:, 1:] will return last columns.
       
    88 
       
    89 c[1:, :2]
       
    90 returns first two columns of last two rows
       
    91 
       
    92 Now we will look into one more powerful feature of arrays: 'striding'.
       
    93 Striding allows us to jump or skip rows or columns by certain interval. We can specify the step size.
       
    94 c[:,:] will give us entire array
       
    95 we add one more ':' to row or column part to specify a step size.
       
    96 c[:, ::2]
       
    97 will give us first and third column. Since step size is two, it start with first column(blank before : means 0) and then we jump one column and then third(blank after : means end)
       
    98 similarly 
       
    99 c[::2,:] returns 2x3 array with first and third row
       
   100 
       
   101 and c[::2, ::2] will give us 2x2 array with first and third row and column 
       
   102 
       
   103 With 
       
   104 
       
   105 ----------------
    35 We have seen 
   106 We have seen 
    36     Welcome to the Tutorial on arrays. 
   107     Welcome to the Tutorial on arrays. 
    37 
   108 
    38     As mentioned in [the previous tutorial] arrays are much faster and
   109     As mentioned in [the previous tutorial] arrays are much faster and
    39     more efficient. In this tutorial we shall look at creating arrays,
   110     more efficient. In this tutorial we shall look at creating arrays,