2014-10-27 48 views
0

我是初學者,使用python。我有以下代碼:ValueError:使用序列符號設置數組元素

import Deformation 
import Qtheta 
import numpy as np 
import sympy as sp 

def champs_de_contraintes(epaisseurs,angles,El,Et,Glt,Nult): 
    E0=np.mat('[0;0;0]') #inizialising E0 
    K=np.mat('[0;0;0]') #inizialising K 
    E0K=np.mat('[0;0;0;0;0;0]') #inizialising K 
    E0K = Deformation.defos(epaisseurs,angles,El,Et,Glt,Nult) #function that return a 6x1 matrix type <class 'numpy.matrixlib.defmatrix.matrix'> 
    E0=E0K[:3] #Slicing E0K into 2 vectors E0 and K 
    K=E0K[3:] 
    nb_composantes = 3 #sigma x, sigma y, taux xy 
    Sigmaxy=np.zeros((nb_composantes,len(epaisseurs))) #sigma x, sigma y, taux xy, Array 

    #===============This bloc calculate the altitude of a ply ================= 

    z=[] 
    e=[] 
    z.append(-(np.sum(epaisseurs))/2) 
    for k in (range(len(epaisseurs))): #initialising e[k] 
     e[len(e):] = [0] #Values to add to get the altitude 
    for i in (range(len(epaisseurs))): 
     e[i]= e[i-1] + epaisseurs[i] #sum e 
     z[len(z):] = [z[0] + e[i]] #Altitude 

    #===========================This bloc calculate a vector=================== 
    for i in (range(len(epaisseurs))): 
     newcolumn= Qtheta.Qtheta(angles[i],El,Et,Glt,Nult)*E0+z[i]*Qtheta.Qtheta(angles[i],El,Et,Glt,Nult)*K #z is the altitude 
     #3x1 = 3x3*3x1 + 1x1*3x3*3x1 
     for m in (range(len(newcolumn))): 
      Sigmaxy[i,m]=newcolumn[m] 
    return Sigmaxy 

這將返回我的錯誤

Sigmaxy[i,m]=newcolumn[m] 
ValueError: setting an array element with a sequence 

基本上,我想要做的是保存矢量「newcolumn」在Sigmaxy矩陣newcolumn。 我認爲我得到這個錯誤是因爲「newcolumn」是符號。實際上,E0和K取決於2個變量。

有人可以幫我這個嗎?

在此先感謝!

回答

1

如果你想以這種方式設置數組的元素,你需要確保形狀是匹配的。

你被拋出,如果你嘗試這樣的錯誤:

my_1d_array = np.array([1,2,3]) 
my_1d_array[0] = [4, 5, 6] 

由於錯誤解釋,你想一個單一的元素設置爲元素的序列,這是不可能的。調試您的代碼,並確保Sigmaxy[i,m]newcolumn[m]的形狀(.shape)匹配。

用正確的大小初始化您的數組,並在開始時爲所有元素留出空間。如果您不知道確切的大小,可以通過添加列來創建更大的陣列,請參閱其他問題。

相關問題