Home > other >  Constructing a matrix defined with an indefinite number of parameters
Constructing a matrix defined with an indefinite number of parameters

Time:05-11

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.

CodePudding user response:

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']]
  • Related