|
1042 | 1042 | "id": "thick-demonstration" |
1043 | 1043 | }, |
1044 | 1044 | "source": [ |
1045 | | - "## Activity: Obtain the transpose of a given matrix" |
| 1045 | + "## Activity 22: Transpose of a Matrix\n", |
| 1046 | + "\n", |
| 1047 | + "Another popular matrix operation is to compute the *transpose*. We flip the elements in the matrix over the diagonal producing a new matrix.\n", |
| 1048 | + "\n", |
| 1049 | + "You are given a 2D list of numbers, `matrix`. How would you write Python code to compute the tranpose of `matrix`?" |
1046 | 1050 | ], |
1047 | 1051 | "id": "thick-demonstration" |
1048 | 1052 | }, |
| 1053 | + { |
| 1054 | + "cell_type": "markdown", |
| 1055 | + "metadata": { |
| 1056 | + "id": "eKYvopLpX1B7" |
| 1057 | + }, |
| 1058 | + "source": [ |
| 1059 | + "### Example Input\n", |
| 1060 | + "\n", |
| 1061 | + "Put another way, the transpose matrix is just what the original `matrix` would be, if you were to read the elements in said `matrix` going up and down vertically, rather than left to right horizontally.\n", |
| 1062 | + "\n", |
| 1063 | + "Therefore, if we had a matrix like the following:\n", |
| 1064 | + "```\n", |
| 1065 | + "matrix = [\n", |
| 1066 | + " [8, 2, 3],\n", |
| 1067 | + " [9, 1, 9], \n", |
| 1068 | + " [5, 4, 1]\n", |
| 1069 | + "]\n", |
| 1070 | + "```\n", |
| 1071 | + "Then we would expect its transpose to be the following output:\n", |
| 1072 | + "```\n", |
| 1073 | + "transposed_matrix = [\n", |
| 1074 | + " [8, 9, 5], # first column\n", |
| 1075 | + " [2, 1, 4], # second column\n", |
| 1076 | + " [3, 9, 1] # third column\n", |
| 1077 | + "]\n", |
| 1078 | + "```\n" |
| 1079 | + ], |
| 1080 | + "id": "eKYvopLpX1B7" |
| 1081 | + }, |
| 1082 | + { |
| 1083 | + "cell_type": "code", |
| 1084 | + "metadata": { |
| 1085 | + "id": "7ooH8oluXyzU" |
| 1086 | + }, |
| 1087 | + "source": [ |
| 1088 | + "matrix = [[8, 2, 3], [9, 1, 9], [5, 4, 1]]" |
| 1089 | + ], |
| 1090 | + "id": "7ooH8oluXyzU", |
| 1091 | + "execution_count": 21, |
| 1092 | + "outputs": [] |
| 1093 | + }, |
| 1094 | + { |
| 1095 | + "cell_type": "markdown", |
| 1096 | + "metadata": { |
| 1097 | + "id": "9lQnNxm-VeaU" |
| 1098 | + }, |
| 1099 | + "source": [ |
| 1100 | + "### Solution " |
| 1101 | + ], |
| 1102 | + "id": "9lQnNxm-VeaU" |
| 1103 | + }, |
1049 | 1104 | { |
1050 | 1105 | "cell_type": "code", |
1051 | 1106 | "metadata": { |
1052 | | - "id": "musical-industry", |
1053 | | - "outputId": "f196159c-a326-4202-f16d-b5de539b0905" |
| 1107 | + "id": "musical-industry" |
1054 | 1108 | }, |
1055 | 1109 | "source": [ |
1056 | | - "def transpose(matx):\n", |
1057 | | - " t_m = [[] for _ in range(len(matx[0]))]\n", |
1058 | | - " # Do not use this: t_m = [[]]*len(matx[0])\n", |
1059 | | - " for i in range(len(matx[0])):\n", |
| 1110 | + "def transpose(matrix):\n", |
| 1111 | + " # A: init the transpose matrix using the dims of the original\n", |
| 1112 | + " transposed_matrix = [[] for _ in range(len(matrix[0]))] \n", |
| 1113 | + " # B: iterate over each column\n", |
| 1114 | + " for col_index in range(len(matrix[0])):\n", |
| 1115 | + " # C: collect the elements in this column\n", |
1060 | 1116 | " for row in matrix:\n", |
1061 | | - " t_m[i].append(row[i])\n", |
1062 | | - " return t_m\n", |
| 1117 | + " transposed_matrix[col_index].append(row[col_index])\n", |
| 1118 | + " # D: return the transpose of the matrix\n", |
| 1119 | + " return transposed_matrix" |
| 1120 | + ], |
| 1121 | + "id": "musical-industry", |
| 1122 | + "execution_count": 22, |
| 1123 | + "outputs": [] |
| 1124 | + }, |
| 1125 | + { |
| 1126 | + "cell_type": "markdown", |
| 1127 | + "metadata": { |
| 1128 | + "id": "UUcNULKQVg1i" |
| 1129 | + }, |
| 1130 | + "source": [ |
| 1131 | + "**Note**: in the solution above, it is also possible to implement step A in the following way:\n", |
1063 | 1132 | "\n", |
| 1133 | + "```\n", |
| 1134 | + "transpose = [[]]*len(matrix[0])\n", |
| 1135 | + "```\n", |
| 1136 | + "However, using a list comprehension with the `range()` function is the more efficient way to initialize our matrix, and thus is preferred. " |
| 1137 | + ], |
| 1138 | + "id": "UUcNULKQVg1i" |
| 1139 | + }, |
| 1140 | + { |
| 1141 | + "cell_type": "markdown", |
| 1142 | + "metadata": { |
| 1143 | + "id": "hJGmQfzfXqnR" |
| 1144 | + }, |
| 1145 | + "source": [ |
| 1146 | + "#### Test Out The Solution" |
| 1147 | + ], |
| 1148 | + "id": "hJGmQfzfXqnR" |
| 1149 | + }, |
| 1150 | + { |
| 1151 | + "cell_type": "code", |
| 1152 | + "metadata": { |
| 1153 | + "colab": { |
| 1154 | + "base_uri": "https://localhost:8080/" |
| 1155 | + }, |
| 1156 | + "id": "B93uZPY0XwrN", |
| 1157 | + "outputId": "0fe978d0-7fd5-49a6-8884-e7445957e73d" |
| 1158 | + }, |
| 1159 | + "source": [ |
1064 | 1160 | "print(transpose(matrix))" |
1065 | 1161 | ], |
1066 | | - "id": "musical-industry", |
1067 | | - "execution_count": null, |
| 1162 | + "id": "B93uZPY0XwrN", |
| 1163 | + "execution_count": 20, |
1068 | 1164 | "outputs": [ |
1069 | 1165 | { |
1070 | 1166 | "output_type": "stream", |
|
0 commit comments