构建使用不确定的参数定义的矩阵
我正在尝试获得值的矩阵。首先,我试图解释(希望清楚地)如何组合矩阵。该矩阵将由具有不确定数量参数的子矩阵形成。 例如,我正在插入一个只有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
逻辑尚不完全清楚,但是要从
sub_m
列表中获取最终输出,您可以使用:输出:
The logic is not fully clear, but to get the final output from the
sub_M
list you can use:output: