Home > Back-end >  How to call specific rows and lines of a matrix and array?
How to call specific rows and lines of a matrix and array?

Time:03-11

I'm developing a code where I have matriz 15x15 and an array 15 lines, but for a calculation, I only want to use the positions [3:12 15], so the matrix will be 11X11 and and array with 11 lines. How can I call just this positions?

kportico = np.matrix([[a1, 0, 0, -a1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, b1, c1, 0, -b1, c1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, c1, d1, 0, -c1, e1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [-a1, 0, 0, a1 a2, 0, 0, -a2, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, -b1, -c1, 0, b1 b2, -c1 c2, 0, -b2, c2, 0, 0, 0, 0, 0, 0],
                      [0, c1, e1, 0, -c1 c2, d1  d2, 0, -c2, e2, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, -a2, 0, 0, a2 a3, 0, 0, -a3, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, -b2, -c2, 0, b2 b3, -c2 c3, 0, -b3, c3, 0, 0, 0],
                      [0, 0, 0, 0, c2, e2, 0, -c2 c3, d2 d3, 0, -c3, e3, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, -a3, 0, 0, a4 a3, 0, 0, -a4, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0 , -b3, -c3, 0, b4 b3, -c4 c3, 0, -b4, c4],
                      [0, 0, 0, 0, 0, 0, 0, c3, e3, 0, -c4 c3, d4 d3, 0, -c4, e4],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, -a4, 0, 0, a4, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -b4, -c4, 0, b4, -c4],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, c4, e4, 0, -c4, d4]])

f = np.matrix([[F1x],
               [F1y],
               [M1],
               [F2x],
               [F2y],
               [M2],
               [F3x],
               [F3y],
               [M3],
               [F4x],
               [F4y],
               [M4],
               [F5x],
               [F5y],
               [M5]])

CodePudding user response:

Let's create the test (smaller) arrays as:

n = 10    # Number of columns / rows
kportico = np.matrix(np.arange(1, 1   n * n).reshape(n, n))
f = np.matrix(np.arange(201, 201   n).reshape(n, 1))

To get indices of some range of rows (in my example [3, 6)) and the last row (9), you can use np.r:

ind = np.r_[3:6, 9]

which yields:

array([3, 4, 5, 9])

Then, to get your slice of kportico, you can:

  • select indicated rows,
  • and from them select indicated columns.

The code to do it is:

kportico[ind][:,ind]

and its result is:

matrix([[ 34,  35,  36,  40],
        [ 44,  45,  46,  50],
        [ 54,  55,  56,  60],
        [ 94,  95,  96, 100]])

And to get the wanted elements from f, run:

f[ind]

which yields:

matrix([[204],
        [205],
        [206],
        [210]])

Note also that matrix indices start from 0 (not from 1), so your target slice shoud probably be:

ind = np.r_[2:12 14]

At least don't attempt to refer to column or row 15, as it will cause IndexError exception.

CodePudding user response:

rotulado = 1
encastrado = 2

base = input("rotulado - 1 ou encastrado - 2?")

if base == rotulado:
    kportico[2:12, 14][2:12, 14]
    f[2:12]

if base == encastrado:
    kportico[3:11][3:11]
    f[3:11]



u = np.dot(kportico, f)
print(u)
  • Related