我有一个二进制值矩阵,大小为211 x 129,例如:
matrix =
(
0 0 1 1 0 0 0 0 0 ... 0 1 1 0 0 0 0
0 0 1 1 0 1 1 0 0 ... 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 ... 0 0 0 0 0 0 0
...
...
...
)
我有一个由211个数字组成的数组:
array =
[
[158 147 35 162 143 139 8 129 43 97 163 151 24 103 161 54 38 10
100 193 192 191 188 187 186 185 184 182 181 179 178 177 176 175 174 171
170 169 167 166 155 154 152 149 148 146 145 142 141 136 134 132 130
....
]
我想将数字与数组中的相应行匹配并创建一个新矩阵。非常重要的是数组中的数字,例如158恰好获得矩阵的第158行。
输出看起来像这样:
new_matrix:
(
158 0 0 0 1 0 0 0 0 0 ... 0 0 1 0 0 0 0 //Values of row 158 from the matrix
147 0 0 1 0 0 1 1 0 0 ... 0 0 0 0 0 0 0 //Values of row 147 from the matrix
35 0 0 1 1 0 0 0 0 0 ... 0 0 0 0 0 0 0 //Values of row 35 from the matrix
162 0 0 0 0 0 0 0 0 1 ... 0 0 0 0 0 1 1
143
...
...
)
有指导吗?
答案 0 :(得分:0)
制作矩阵:
matrix = [[] for _ in range(211)]
现在,您可以填充它:
for row in enumerate(array):
matrix[row[0]] = old_matrix[row[1]]
答案 1 :(得分:0)
怎么办
matrix[array - 1, :]
array - 1
代表indexing is 0-based in Python的事实。
>>> matrix = np.random.rand(5, 4)
>>> matrix
array([[0.15894248, 0.21096647, 0.5282654 , 0.69521 ],
[0.86969885, 0.36367184, 0.3805844 , 0.3208523 ],
[0.86888592, 0.40990308, 0.73697685, 0.5205354 ],
[0.56091075, 0.92830105, 0.63612971, 0.54486469],
[0.33106509, 0.12822892, 0.30061825, 0.03357865]])
>>> array = np.random.randint(1, 5, 3)
>>> array
array([1, 1, 4])
最后
>>> matrix[array - 1, :]
array([[0.15894248, 0.21096647, 0.5282654 , 0.69521 ],
[0.15894248, 0.21096647, 0.5282654 , 0.69521 ],
[0.56091075, 0.92830105, 0.63612971, 0.54486469]])