[SOLVED] How to use all the elements of the array using for loop?

Issue

actually, I need to put the returned values of the function (global_displacement(X)) into another running loop.

can someone please tell me how to obtain the required output?

and what idiotic mistake I have been doing.

as every time it gives me only the first([ 0, 0, X[0], X[1]]) OR

the last value([ X[20], X[21], X[53], X[54]]) in the output,

because of wrong indendation of "return j" in the below written code .

import numpy as np 


X = [ 0.19515612,  0.36477665,  0.244737,    0.42873321, 0.16864666,  0.08636661,  0.05376605, -0.57201897, -0.00935055, -1.24923862,  0.,         -1.53111525,  0.00935055, -1.24923862, -0.05376605, -0.57201897, -0.1686466,
     0.08636661, -0.244737,    0.42873321, -0.19515612,  0.36477665,  0.02279911,  0.  ,        0.3563355 ,  0.01379104,  0.   ,       0.42289958, -0.00747999,  0.   ,       0.0825908,  -0.02949519 , 0.   ,      -0.57435396,
     -0.04074819,  0.   ,      -1.25069528 ,-0.02972642,  0.    ,     -1.53227704, -0.    ,      0.   ,      -1.25069528 , 0.02972642 , 0.   ,      -0.57435396 , 0.04074819 , 0.     ,     0.0825908,   0.02949519,  0.  ,
        0.42289958,  0.00747999 , 0.      ,    0.3563355 , -0.01379104, -0.02279911]



def global_displacement(X):
    
    global_displacements = np.array( [[ 0,  0, X[0],  X[1]], [ X[0], X[1], X[2], X[3]], [ X[2],  X[3],X[4],  X[5]], [ X[4],X[5],X[6], X[7]],[ X[6],X[7],X[8],X[9]], [ X[8],X[9],X[10], X[11] ], [ X[10], X[11],X[12], X[13]], [ X[12], X[13],X[14], X[15]],[ X[14], X[15],X[16], X[17]],[ X[16], X[17],X[18], X[19]], [ X[18], X[19],X[20], X[21]],[ X[20], X[21], 0, 0],
                            [ X[0], X[1], X[23], X[24]], [ X[2], X[3], X[26],X[27]], [ X[4], X[5], X[29],X[30]], [ X[6], X[7], X[32],X[33]], [ X[8],X[9],X[35], X[36]], [ X[10], X[11], X[38], X[39]], [ X[12], X[13], X[41], X[42]] ,[ X[14], X[15], X[44], X[45]],[ X[16], X[17], X[47], X[48]],[ X[18], X[19], X[50], X[51]], [ X[20], X[21], X[53], X[54]] ] )
    
    for i in (global_displacements):
        j =  i.reshape(4,1)
        return j

print(global_displacement(X))

this is the expected output, and I need to put these values in another loop, by calling this function.

[[0.        ]
 [0.        ]
 [0.19515612]
 [0.36477665]]
[[0.19515612]
 [0.36477665]
 [0.244737  ]
 [0.42873321]]
[[0.244737  ]
 [0.42873321]
 [0.16864666]
 [0.08636661]]
[[ 0.16864666]
 [ 0.08636661]
 [ 0.05376605]
 [-0.57201897]]
[[ 0.05376605]
 [-0.57201897]
 [-0.00935055]
 [-1.24923862]]
[[-0.00935055]
 [-1.24923862]
 [ 0.        ]
 [-1.53111525]]
[[ 0.        ]
 [-1.53111525]
 [ 0.00935055]
 [-1.24923862]]
[[ 0.00935055]
 [-1.24923862]
 [-0.05376605]
 [-0.57201897]]
[[-0.05376605]
 [-0.57201897]
 [-0.1686466 ]
 [ 0.08636661]]
[[-0.1686466 ]
 [ 0.08636661]
 [-0.244737  ]
 [ 0.42873321]]
[[-0.244737  ]
 [ 0.42873321]
 [-0.19515612]
 [ 0.36477665]]
[[-0.19515612]
 [ 0.36477665]
 [ 0.        ]
 [ 0.        ]]
[[0.19515612]
 [0.36477665]
 [0.        ]
 [0.3563355 ]]
[[0.244737  ]
 [0.42873321]
 [0.        ]
 [0.42289958]]
[[0.16864666]
 [0.08636661]
 [0.        ]
 [0.0825908 ]]
[[ 0.05376605]
 [-0.57201897]
 [ 0.        ]
 [-0.57435396]]
[[-0.00935055]
 [-1.24923862]
 [ 0.        ]
 [-1.25069528]]
[[ 0.        ]
 [-1.53111525]
 [ 0.        ]
 [-1.53227704]]
[[ 0.00935055]
 [-1.24923862]
 [ 0.        ]
 [-1.25069528]]
[[-0.05376605]
 [-0.57201897]
 [ 0.        ]
 [-0.57435396]]
[[-0.1686466 ]
 [ 0.08636661]
 [ 0.        ]
 [ 0.0825908 ]]
[[-0.244737  ]
 [ 0.42873321]
 [ 0.        ]
 [ 0.42289958]]
[[-0.19515612]
 [ 0.36477665]
 [ 0.        ]
 [ 0.3563355 ]]

Solution

Your function already converts everything into the right format except that the inner values should be stored into a list. For this you can use numpy.newaxis. It is used to add a new dimension to your array (good post about its functionality).

import numpy as np

def global_displacement(X):
    global_displacements = np.array( [[ 0,  0, X[0],  X[1]], [ X[0], X[1], X[2], X[3]], [ X[2],  X[3],X[4],  X[5]], [ X[4],X[5],X[6], X[7]],[ X[6],X[7],X[8],X[9]], [ X[8],X[9],X[10], X[11] ], [ X[10], X[11],X[12], X[13]], [ X[12], X[13],X[14], X[15]],[ X[14], X[15],X[16], X[17]],[ X[16], X[17],X[18], X[19]], [ X[18], X[19],X[20], X[21]],[ X[20], X[21], 0, 0],
                                [ X[0], X[1], X[23], X[24]], [ X[2], X[3], X[26],X[27]], [ X[4], X[5], X[29],X[30]], [ X[6], X[7], X[32],X[33]], [ X[8],X[9],X[35], X[36]], [ X[10], X[11], X[38], X[39]], [ X[12], X[13], X[41], X[42]] ,[ X[14], X[15], X[44], X[45]],[ X[16], X[17], X[47], X[48]],[ X[18], X[19], X[50], X[51]], [ X[20], X[21], X[53], X[54]] ] )

    new_structure = global_displacements[:, :, np.newaxis]   
    return new_structure

X = [ 0.19515612,  0.36477665,  0.244737,    0.42873321, 0.16864666,  0.08636661,  0.05376605, -0.57201897, -0.00935055, -1.24923862,  0.,         -1.53111525,  0.00935055, -1.24923862, -0.05376605, -0.57201897, -0.1686466,
     0.08636661, -0.244737,    0.42873321, -0.19515612,  0.36477665,  0.02279911,  0.  ,        0.3563355 ,  0.01379104,  0.   ,       0.42289958, -0.00747999,  0.   ,       0.0825908,  -0.02949519 , 0.   ,      -0.57435396,
     -0.04074819,  0.   ,      -1.25069528 ,-0.02972642,  0.    ,     -1.53227704, -0.    ,      0.   ,      -1.25069528 , 0.02972642 , 0.   ,      -0.57435396 , 0.04074819 , 0.     ,     0.0825908,   0.02949519,  0.  ,
        0.42289958,  0.00747999 , 0.      ,    0.3563355 , -0.01379104, -0.02279911]

result = global_displacement(X)

print(result)

Output:

[[[ 0.        ]
  [ 0.        ]
  [ 0.19515612]
  [ 0.36477665]]

 [[ 0.19515612]
  [ 0.36477665]
  [ 0.244737  ]
  [ 0.42873321]]

 [[ 0.244737  ]
  [ 0.42873321]
  [ 0.16864666]
  [ 0.08636661]]

 [[ 0.16864666]
  [ 0.08636661]
  [ 0.05376605]
  [-0.57201897]]

 [[ 0.05376605]
  [-0.57201897]
  [-0.00935055]
  [-1.24923862]]

 [[-0.00935055]
  [-1.24923862]
  [ 0.        ]
  [-1.53111525]]

 [[ 0.        ]
  [-1.53111525]
  [ 0.00935055]
  [-1.24923862]]

 [[ 0.00935055]
  [-1.24923862]
  [-0.05376605]
  [-0.57201897]]

 [[-0.05376605]
  [-0.57201897]
  [-0.1686466 ]
  [ 0.08636661]]

 [[-0.1686466 ]
  [ 0.08636661]
  [-0.244737  ]
  [ 0.42873321]]

 [[-0.244737  ]
  [ 0.42873321]
  [-0.19515612]
  [ 0.36477665]]

 [[-0.19515612]
  [ 0.36477665]
  [ 0.        ]
  [ 0.        ]]

 [[ 0.19515612]
  [ 0.36477665]
  [ 0.        ]
  [ 0.3563355 ]]

 [[ 0.244737  ]
  [ 0.42873321]
  [ 0.        ]
  [ 0.42289958]]

 [[ 0.16864666]
  [ 0.08636661]
  [ 0.        ]
  [ 0.0825908 ]]

 [[ 0.05376605]
  [-0.57201897]
  [ 0.        ]
  [-0.57435396]]

 [[-0.00935055]
  [-1.24923862]
  [ 0.        ]
  [-1.25069528]]

 [[ 0.        ]
  [-1.53111525]
  [ 0.        ]
  [-1.53227704]]

 [[ 0.00935055]
  [-1.24923862]
  [ 0.        ]
  [-1.25069528]]

 [[-0.05376605]
  [-0.57201897]
  [ 0.        ]
  [-0.57435396]]

 [[-0.1686466 ]
  [ 0.08636661]
  [ 0.        ]
  [ 0.0825908 ]]

 [[-0.244737  ]
  [ 0.42873321]
  [ 0.        ]
  [ 0.42289958]]

 [[-0.19515612]
  [ 0.36477665]
  [ 0.        ]
  [ 0.3563355 ]]]

Answered By – JANO

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.