有没有从Nx3矩阵中提取特定数据的算法

2024-06-24 13:11:46 发布

您现在位置:Python中文网/ 问答频道 /正文

问题是:我不知道如何继续实现下面的模式(嗯,我尝试了很多方法,但都没有成功)


我有一个matrixNx3(RowxCol):[[1,2,3], [4,5,6], [7,8,9]]

视觉上应该是这样的:

|01, 02, 03| ----- > |M(00), M(01), M(02)|
|04, 05, 06| ----- > |M(10), M(11), M(12)|
|07, 08, 09| ----- > |M(10), M(21), M(22)|

我需要按照这个模式检索一些数据来得到matrix(2N)xN:
[[02, 04, 07], [03, 04, 07], [01, 05, 07], [01, 06, 07], [01, 04, 08], [01, 04, 09]]

视觉上:

|02, 04, 07| ----- > Second, First,  First (of each row)
|03, 04, 07| ----- > Third,  First,  First
|01, 05, 07| ----- > First,  Second, First
|01, 06, 07| ----- > First,  Third,  First
|01, 04, 08| ----- > First,  First,  Second
|01, 04, 09| ----- > First,  First,  Third

其思想是从每行中获取第二个和第三个值,并用每行中的第一个值来完成行中其余的空间。我说Nx3是因为N可以增加,但是我是如何被3x3绊倒的,我就用了这个例子


Tags: of数据方法模式视觉matrixrow思想
1条回答
网友
1楼 · 发布于 2024-06-24 13:11:46

我认为解决方案相当简单,如果您可以循环一次N

import numpy as np
N=4  # set some arbitrary value for the dimension
M=np.reshape(np.arange(N*3), (N,3))  # create a dummy matrix
out=np.zeros((2*N,N)) # output matrix with size 2N x N, all zeros
out[:,:]=M[:,0]  # fill each column of the matrix with the first entry of M's rows
for i in range(N): 
    out[2*i,i]=M[i,1] # fill in the second value of M at position (2i, i)
    out[2*i+1,i]=M[i,2] # fill in the third value of M at position (2i+1, i)

相关问题 更多 >