构建使用不确定的参数定义的矩阵

发布于 2025-01-27 15:00:38 字数 1666 浏览 1 评论 0原文

我正在尝试获得值的矩阵。首先,我试图解释(希望清楚地)如何组合矩阵。该矩阵将由具有不确定数量参数的子矩阵形成。 例如,我正在插入一个只有3个参数的子序列:

a1, a2, a3, b1, b2, b3

从这些值中,我想构造类型的矩阵:

[['a1', 'a2', 'a3'],
 ['b1', 'a2', 'a3'],
 ['a1', 'b2', 'a3'],
 ['a1', 'a2', 'b3']]

从第二行开始,我将具有值的对角线 b

我的目标是创建一个脚本,该脚本可以根据传递的参数数量而定,但遵循相同的逻辑。

我已经构造了一个示例:

RDesign = { 
"par1" : {"a": ["a11", "a12", "a13"],
        "b": ["b11", "b12", "b13"]},
"par2" : {"a": ["a21", "a22", "a23"],
        "b": ["b21", "b22", "b23"]},
"par3" : {"a": ["a31", "a32", "a33"],
        "b": ["b31", "b32", "b33"]},
}

该字典类似于我使用的字典,它表明A1和B1是相同参数值(PAR1)值的一部分。对于我要求的目的而言,这并不重要。 在此示例情况下,我有3个参数(PAR1,PAR2,PAR3),分配了两个类别的值“ A”和“ B”。示例案例包含几个值(A11,A12等),但为简单起见,我只会考虑第一个[0](A11,B11,B21 ... B31)。

的值,

def submat(dict_):
    sub_matrix = []
    for key in dict_:
        A = dict_[key]["a"][0]
        sub_matrix.append(A)
    for key in dict_:
        B = dict_[key]["b"][0]
        sub_matrix.append(B)
    return sub_matrix

此函数生成了subbatrix: sub_m = subbat(rdesign)

sub_M

Out[84]: ['a11', 'a21', 'a31', 'b11', 'b21', 'b31']

以阐明我要实现的结果,因此,我通过“手动”输入值创建了一个数组。

iteration = [[sub_M[0], sub_M[1], sub_M[2]], 
    [sub_M[3], sub_M[1], sub_M[2]],
    [sub_M[0], sub_M[4], sub_M[2]],
    [sub_M[0], sub_M[1], sub_M[5]]]

因此,结果最终应该看起来像这样:

iteration
Out[85]: 
[['a11', 'a21', 'a31'],
 ['b11', 'a21', 'a31'],
 ['a11', 'b21', 'a31'],
 ['a11', 'a21', 'b31']]

目的是以自动方式创建此类型的矩阵,并且在传递几个参数时也遵循相同的逻辑。我希望我已经清楚自己,但是我准备修改信息以尝试更好地解释它。

I am trying to obtain a matrix of values. I start by trying to explain (hopefully clearly) how the matrix will need to be composed. The matrix is to be formed by a sub-matrix with an indefinite number of parameters.
As an example, I am inserting a submatrix with only 3 parameters:

a1, a2, a3, b1, b2, b3

From these values I would then like to construct a matrix of the type:

[['a1', 'a2', 'a3'],
 ['b1', 'a2', 'a3'],
 ['a1', 'b2', 'a3'],
 ['a1', 'a2', 'b3']]

Starting from the second line I will have a diagonal of values b.

My goal is to create a script that can build from the submatrix to the matrix depending on the number of parameters passed, but following the same logic.

I have constructed an example dict:

RDesign = { 
"par1" : {"a": ["a11", "a12", "a13"],
        "b": ["b11", "b12", "b13"]},
"par2" : {"a": ["a21", "a22", "a23"],
        "b": ["b21", "b22", "b23"]},
"par3" : {"a": ["a31", "a32", "a33"],
        "b": ["b31", "b32", "b33"]},
}

This dictionary is similar to the one I am using and it shows that a1 and b1 are part of values of the same parameter (par1). This is not important for the purposes of what I am requesting.
In this example case I have 3 parameters (par1, par2, par3) to which two categories of values "a" and "b" are assigned. The example case contains several values (a11, a12, etc.) but for simplicity I will only consider the first one [0] (a11, b11, b21 ... b31).

this function generates the values of the submatrix:

def submat(dict_):
    sub_matrix = []
    for key in dict_:
        A = dict_[key]["a"][0]
        sub_matrix.append(A)
    for key in dict_:
        B = dict_[key]["b"][0]
        sub_matrix.append(B)
    return sub_matrix

sub_M = submat(RDesign)

sub_M

Out[84]: ['a11', 'a21', 'a31', 'b11', 'b21', 'b31']

To clarify the result I want to achieve, I have therefore created an array by entering the values "manually".

iteration = [[sub_M[0], sub_M[1], sub_M[2]], 
    [sub_M[3], sub_M[1], sub_M[2]],
    [sub_M[0], sub_M[4], sub_M[2]],
    [sub_M[0], sub_M[1], sub_M[5]]]

So ultimately the result should look something like this:

iteration
Out[85]: 
[['a11', 'a21', 'a31'],
 ['b11', 'a21', 'a31'],
 ['a11', 'b21', 'a31'],
 ['a11', 'a21', 'b31']]

The aim would be to create a matrix of this type in an automatic manner and which also follows the same logic when several parameters are passed. I hope I have made myself clear, but I am ready to modify the message to try to explain it better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2025-02-03 15:00:38

逻辑尚不完全清楚,但是要从sub_m列表中获取最终输出,您可以使用:

import numpy as np
sub_M = ['a11', 'a21', 'a31', 'b11', 'b21', 'b31']

N = 3  # half size of sub_M, can be computed as len(sub_M)//2

a = np.tile(sub_M[:N], (N+1,1))
np.fill_diagonal(a, sub_M[N:])
a = np.roll(a, 1, axis=0)

输出:

[['a11' 'a21' 'a31']
 ['b11' 'a21' 'a31']
 ['a11' 'b21' 'a31']
 ['a11' 'a21' 'b31']]

The logic is not fully clear, but to get the final output from the sub_M list you can use:

import numpy as np
sub_M = ['a11', 'a21', 'a31', 'b11', 'b21', 'b31']

N = 3  # half size of sub_M, can be computed as len(sub_M)//2

a = np.tile(sub_M[:N], (N+1,1))
np.fill_diagonal(a, sub_M[N:])
a = np.roll(a, 1, axis=0)

output:

[['a11' 'a21' 'a31']
 ['b11' 'a21' 'a31']
 ['a11' 'b21' 'a31']
 ['a11' 'a21' 'b31']]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文