英文:
Conversion of a set of position vectors into a matrix of ones
问题
以下是翻译好的部分:
这是我拥有的输入
{(0, 1), (2, 1), (0, 0), (1, 1), (1, 0)}
这些是矩阵大小为N=5的矩阵中的1的位置。
我期望的输出是
[[1,1,0,0,0],
[1,1,0,0,0],
[0,1,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]]
是否有一种有效的方法来进行转换?也许有一个内置的numpy函数吗?创建一个全零矩阵,然后循环遍历位置向量以插入1似乎对我来说不太有效。
我的最终目标是将输出传递给matplotlib的matshow()函数。
英文:
Here is the input that I have
{(0, 1), (2, 1), (0, 0), (1, 1), (1, 0)}
These are the positions of the ones in a matrix of size, N=5.
My desired output is
[[1,1,0,0,0],
[1,1,0,0,0],
[0,1,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]]
Is there an efficient way to do the conversion? Perhaps a builtin numpy function? Creating a matrix of zeroes then cycling through the position vectors to push in the ones doesn't seem very efficient to me.
My ultimate goal with the output is to feed it to matplotlib's matshow() function.
答案1
得分: 3
@Ansagara的回答完成了任务,但需要使用for循环。也可以在不使用for循环的情况下完成,如下所示。对于较大的positions
数组,这种方法更具扩展性。
import numpy as np
positions = {(0, 1), (2, 1), (0, 0), (1, 1), (1, 0)}
N = 5
# 创建一个零矩阵
matrix = np.zeros((N, N), dtype=int)
# 根据位置向量在位置上分配1
positions = np.asarray(list(positions))
matrix[positions[:,0], positions[:,1]] = 1
print(matrix)
这将得到
[[1 1 0 0 0]
[1 1 0 0 0]
[0 1 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
英文:
@Ansagara's answer does the job, but requires a for loop. It can also be done without for loops as given below. This scales better for larger positions
arrays.
import numpy as np
positions = {(0, 1), (2, 1), (0, 0), (1, 1), (1, 0)}
N = 5
# Create a matrix of zeros
matrix = np.zeros((N, N), dtype=int)
# Assign 1 to the positions based on the position vectors
positions = np.asarray(list(positions))
matrix[positions[:,0], positions[:,1]] = 1
print(matrix)
Which gives
[[1 1 0 0 0]
[1 1 0 0 0]
[0 1 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
答案2
得分: 1
你可以使用numpy.zeros()函数创建一个零矩阵,然后根据位置向量的位置分配值1。
这是一个演示这种方法的示例代码:
import numpy as np
positions = {(0, 1), (2, 1), (0, 0), (1, 1), (1, 0)}
N = 5
# 创建一个零矩阵
matrix = np.zeros((N, N), dtype=int)
# 根据位置向量分配1
for pos in positions:
matrix[pos] = 1
print(matrix)
输出结果:
[[1 1 0 0 0]
[1 1 0 0 0]
[0 1 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
英文:
You can utilize the numpy.zeros() function to create a matrix of zeros and then assign the value of 1 to the corresponding positions based on the position vectors.
Here's an example code that demonstrates this approach:
import numpy as np
positions = {(0, 1), (2, 1), (0, 0), (1, 1), (1, 0)}
N = 5
# Create a matrix of zeros
matrix = np.zeros((N, N), dtype=int)
# Assign 1 to the positions based on the position vectors
for pos in positions:
matrix[pos] = 1
print(matrix)
Output:
[[1 1 0 0 0]
[1 1 0 0 0]
[0 1 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
答案3
得分: 1
使用NumPy的广播(broadcast)来设置值可能比使用for循环更好。首先,让我们获取每行的列集合,它被称为"dic"。
0 2 1是行,0:[1,0]是第一行索引的列索引。
2:[1]是第三行索引的列索引。
1:[1, 0]是第二行索引的列索引。
使用"dic",你可以使用广播来在矩阵中设置值。
dic: {0: [1, 0], 2: [1], 1: [1, 0]}
import numpy as np
kk = [(0, 1), (2, 1), (0, 0), (1, 1), (1, 0)]
# kk = sorted(kk.__iter__(), key=lambda j:j[0])
dic = {}
for i in kk:
if i[0] not in dic.keys():
dic[i[0]] = [i[1]]
else:
dic[i[0]].append(i[1])
zer = np.zeros((6,6))
for key, val in dic.items():
zer[key, val] = 1
zer
array([[1., 1., 0., 0., 0., 0.],
[1., 1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
英文:
using the numpy broadcast to set value.it may be better than the for loop.<br>
firstly, let us get the each row collection of columns, it is "dic".<br>
0 2 1 is the row, 0: [1,0] is the column index of the first row index.<br><br>
2: [1] is the column index of the third row index.<br>
1: [1, 0] is the column index of the second row index.<br>
with dic, you can using the boardcast to set value in the matrix<br>
dic: {0: [1, 0], 2: [1], 1: [1, 0]}
import numpy as np
kk = [(0, 1), (2, 1), (0, 0), (1, 1), (1, 0)]
# kk = sorted(kk.__iter__(), key=lambda j:j[0])
dic = {}
for i in kk:
if i[0] not in dic.keys():
dic[i[0]] = [i[1]]
else:
dic[i[0]].append(i[1])
zer = np.zeros((6,6))
for key, val in dic.items():
zer[key, val] = 1
zer
array([[1., 1., 0., 0., 0., 0.],
[1., 1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论