简体   繁体   English

arrays中如何按特定顺序赋值

[英]How to assign values in arrays according to a specific sequence

I try to assign a value from the second matrix to each value from the first matrix according to the procedure.我尝试根据过程将第二个矩阵中的一个值分配给第一个矩阵中的每个值。 I assign a value to each value in the first row, then to each value in the last column.我为第一行中的每个值分配一个值,然后为最后一列中的每个值分配一个值。 Then I assign a value in the second row, just like in the penultimate column, but the first and last values from the second matrix are omitted.然后我在第二行中分配一个值,就像在倒数第二列中一样,但是第二个矩阵的第一个和最后一个值被省略了。 That is, and until each value from the first matrix is assigned a value from the second matrix.也就是说,直到第一个矩阵的每个值都被分配一个来自第二个矩阵的值。

It is better to see this in the example below.最好在下面的示例中看到这一点。 example例子

Input输入

a = np.array([[11,12,13],
              [21,22,23],
              [31,32,33],
              [41,42,43],
              [51,52,53]])

b= np.array([100,200,300,400,500,600,700])

required output需要 output

11 100
12 200
13 300
23 400
33 500
43 600
53 700
21 200
22 300
32 400
42 500
52 600

31 300
41 400
51 500

It is possible?有可能的?

I was able to reproduce your required output for the given input with the following code:我能够使用以下代码为给定输入重现您所需的 output:

import numpy as np

def matrix_assing(a, b):
    output = []
    
    for i in range(len(a[0])):
        horizontal_values_to_assign = a[i][: len(a[i]) - i]
        vertical_values_to_assing = a.T[-(i+1)][i+1:]
        
        values_to_assign = np.append(horizontal_values_to_assign, vertical_values_to_assing)
        
        output += list(zip(values_to_assign, b[i:len(b) - i]))
        
    return output

a = np.array([[11,12,13],
              [21,22,23],
              [31,32,33],
              [41,42,43],
              [51,52,53]])

b = np.array([100,200,300,400,500,600,700])

matrix_assing(a, b)

Could you please be more specific what should happen when the length of b does not fit around the sides of the matrix a ( len(b).= len(a[0]) + len(aT[0]) - 1 ).您能否更具体地说明当 b 的长度不适合矩阵 a 的两侧时会发生什么( len(b).= len(a[0]) + len(aT[0]) - 1 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM