arrays.txt
changeset 68 fe5d3fb83597
parent 67 806cca3b7231
child 70 c172d36d9a76
equal deleted inserted replaced
67:806cca3b7231 68:fe5d3fb83597
    38 c
    38 c
    39 
    39 
    40 to access the element 23 we type
    40 to access the element 23 we type
    41 c[1][2]
    41 c[1][2]
    42 
    42 
    43 It is second row third column of c. Note that index values of arrays also start from 0.
    43 It is the 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
    44 Alternative and popular way of doing this is
    45 c[1, 2]
    45 c[1, 2]
    46 
    46 
    47 here ',' is used as separator for row and column value. Similarly any value from the array can be accessed.
    47 here ',' is used as separator for row and column value. Similarly any value from the array can be accessed.
    48 
    48 
    60 as we can see, second row all elements are now 0
    60 as we can see, second row all elements are now 0
    61 
    61 
    62 Accessing a row is straight forward we skip column part
    62 Accessing a row is straight forward we skip column part
    63 but to access whole column we have to use
    63 but to access whole column we have to use
    64 c[:,2]
    64 c[:,2]
    65 will return third column.
    65 it returns third column.
    66 here the ':' part mentioned for row value symbolises entire row.
    66 here the ':' part mentioned for row value means entire row.
    67 the c[1] we were using earlier can also be written as c[1,:]
    67 the c[1] we were using earlier can also be written as c[1,:]
    68 
    68 
    69 ':' actually takes two value. for any row or column we can mention
    69 ':' actually takes two value. for any row or column we can mention start:end values, and rows/columns starting for 'start' till 'end' will be returned. Lets try some examples for better understanding
    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,:]
    70 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.
    71 will result in 2x3 array with 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 
    72 
    74 c[1:3,:] 
    73 c[1:3,:] 
    75 gives second and third row.
    74 gives second and third row.
    76 
    75 
    77 similarly we can try this on columns also:
    76 similarly we can try this on columns also:
    81 There is one more interesting and handy feature of slicing. We saw earlier that how only ':' means entire row or column.
    80 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.
    81 It actually means if we don't specify start and end part of slice default is from zero to end.
    83 
    82 
    84 so
    83 so
    85 c[:, :2]
    84 c[:, :2]
    86 will also give us first two columns
    85 will also give us the first two columns
    87 and c[:, 1:] will return last columns.
    86 and c[:, 1:] will return last columns of c
    88 
    87 
    89 c[1:, :2]
    88 c[1:, :2]
    90 returns first two columns of last two rows
    89 returns first two columns of last two rows
    91 
    90 
    92 Now we will look into one more powerful feature of arrays: 'striding'.
    91 Now we will look into one more powerful feature of arrays: 'striding'.