changeset 340 | 347ff2714deb |
parent 328 | 4075482a9770 |
child 341 | 7ae88b9da553 |
329:0a6ab1d81491 | 340:347ff2714deb |
---|---|
26 \section{Matrices} |
26 \section{Matrices} |
27 \subsection{Basics} |
27 \subsection{Basics} |
28 Matrix Creation\\ |
28 Matrix Creation\\ |
29 \typ{In []: C = array([[1,1,2], [2,4,1], [-1,3,7]])}\\ |
29 \typ{In []: C = array([[1,1,2], [2,4,1], [-1,3,7]])}\\ |
30 It creates C matrix of shape 3x3\\ |
30 It creates C matrix of shape 3x3\\ |
31 Shape is dimenions of given array. |
31 Shape is dimensions of given array. |
32 \begin{lstlisting} |
32 \begin{lstlisting} |
33 In []: C.shape |
33 In []: C.shape |
34 Out[]: (3, 3) |
34 Out[]: (3, 3) |
35 In []: shape([[1,2],[4,5],[3,0]]) |
35 In []: shape([[1,2],[4,5],[3,0]]) |
36 Out[]: (3, 2) |
36 Out[]: (3, 2) |
50 [ 2, 4, 1], |
50 [ 2, 4, 1], |
51 [-1, 3, 7]]) |
51 [-1, 3, 7]]) |
52 In []: C[1,2] |
52 In []: C[1,2] |
53 Out[]: 1 |
53 Out[]: 1 |
54 \end{lstlisting} |
54 \end{lstlisting} |
55 Two indexes seperated by \typ{','} specifies [row, column]. So \typ{C[1,2]} gets third element of second row(indices starts from 0). |
55 Two indexes separated by \typ{','} specifies [row, column]. So \typ{C[1,2]} gets third element of second row(indices starts from 0). |
56 \newpage |
56 \newpage |
57 \begin{lstlisting} |
57 \begin{lstlisting} |
58 In []: C[1] |
58 In []: C[1] |
59 Out[]: array([2, 4, 1]) |
59 Out[]: array([2, 4, 1]) |
60 \end{lstlisting} |
60 \end{lstlisting} |
75 [ 0, 0, 0], |
75 [ 0, 0, 0], |
76 [-1, 3, 7]]) |
76 [-1, 3, 7]]) |
77 \end{lstlisting} |
77 \end{lstlisting} |
78 |
78 |
79 \subsection{Slicing} |
79 \subsection{Slicing} |
80 Accessing rows with Matricies is straightforward. But If one wants to access particular Column, or want a sub-matrix, Slicing is the way to go. |
80 Accessing rows with Matrices is straightforward. But If one wants to access particular Column, or want a sub-matrix, Slicing is the way to go. |
81 \begin{lstlisting} |
81 \begin{lstlisting} |
82 In []: C[:,1] |
82 In []: C[:,1] |
83 Out[]: array([1, 0, 3]) |
83 Out[]: array([1, 0, 3]) |
84 \end{lstlisting} |
84 \end{lstlisting} |
85 First index(:) specifies row(':' implies all the rows) and second index(1) specifies column(second column). |
85 First index(:) specifies row(':' implies all the rows) and second index(1) specifies column(second column). |
121 \end{lstlisting} |
121 \end{lstlisting} |
122 \typ{'1:'} => Start from second row, till last row\\ |
122 \typ{'1:'} => Start from second row, till last row\\ |
123 \typ{':2'} => Start from first column, till and excluding third column. |
123 \typ{':2'} => Start from first column, till and excluding third column. |
124 \newpage |
124 \newpage |
125 \subsection{Striding} |
125 \subsection{Striding} |
126 Often apart from submatrix, one needs to get some mechanism to jump a step. For example, how can we have all alternate rows of a Matrix. \\ |
126 Often apart from sub-matrix, one needs to get some mechanism to jump a step. For example, how can we have all alternate rows of a Matrix. \\ |
127 Following method will return Matrix with alternate rows. |
127 Following method will return Matrix with alternate rows. |
128 \begin{lstlisting} |
128 \begin{lstlisting} |
129 In []: C[::2,:] |
129 In []: C[::2,:] |
130 Out[]: |
130 Out[]: |
131 array([[ 1, 1, 2], |
131 array([[ 1, 1, 2], |