循环中数组的拼接,希望可以拼成一个大数组

问题遇到的现象和发生背景

在做岩石DIC分析的时候,在处理一组数据

问题相关代码,请勿粘贴截图

from scipy import signal
import numpy as np
import pandas as pd
import lvreader as lv
import csv
for i in range(0,7133,10):
 b = lv.read_set("E:\\ypj\\Processing_subset=45_step=15_2022-01-10_01.set")[5]
 arr = b.as_masked_array()
 buffer = b
 frame = buffer[0]
 depth = len(frame)
 height, width = frame.shape
 planes = [frame.as_masked_array(plane=i) for i in range(depth)]
 u = np.ma.array([plane["u"] for plane in planes])
 v = np.ma.array([plane["v"] for plane in planes])
 w = np.ma.array([plane["w"] for plane in planes])
 x, y, z = np.meshgrid(np.linspace(0, 1, depth),np.linspace(0, 1, height),np.linspace(0, 1, width),indexing="ij")
 assert u.shape == x.shape == (depth, height, width)
 XX = x.ravel()
 YY = y.ravel()
 ZZ = z.ravel()
 UU = u.ravel()
 #VV = v.ravel()
 #WW = w.ravel()
 #m = np.array([[['','','','']]])
 m=[]
 for j in range(0, 25098):  #25098
   if(ZZ[j]>=1 and YY[j]>=1):
       l= np.dstack((XX[j], YY[j], ZZ[j], UU[j]))
       m.append(l[j])
       #print(m)
       #m= l.append(l)
       #print(m)
       #print(ZZ[j])
      #print(np.min(ZZ, axis=0))
      #print(np.min(YY, axis=0))
#m = np.append(l)
#m= np.empty((25098,1,4))
#m= np.vstack(l[j])
ll = np.mat(l)
       # f = open("result locate.csv", "w", newline="")
       # csv_writer = csv.writer(f, dialect="excel")
       # csv_writer.writerow(ll)
       # df = pd.DataFrame(ll)
kernel = np.ones((4, 4), dtype=None)
       # np.set_printoptions(threshold=np.inf)
       # 求方差
S = np.var(ll)
       # print("方差为:%f" % S)
Xstart = signal.convolve2d(ll, kernel, mode='full')
       # print(Xstart)
Ws = np.var(Xstart)
       # print("处理后方差为:%f" % Ws)
Sw = Ws * S
print("统计指标为:%f" % Sw)

运行结果及报错内容
C:\Users\Windows10\AppData\Local\Programs\Python\Python38\python.exe C:/Users/Windows10/AppData/Roaming/JetBrains/PyCharmCE2021.3/scratches/ypj.py
Traceback (most recent call last):
  File "C:/Users/Windows10/AppData/Roaming/JetBrains/PyCharmCE2021.3/scratches/ypj.py", line 30, in <module>
    m.append(l[j])
  File "C:\Users\Windows10\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\ma\core.py", line 3222, in __getitem__
    dout = self.data[indx]
IndexError: index 25097 is out of bounds for axis 0 with size 1
我的解答思路和尝试过的方法

我尝试各种数组拼接方式,但是还是有问题,这个问题困扰好几天了

我想要达到的结果

我想要把if循环筛选出来的数据拼接成一个大数组,然后进行下面的运算

跟m添加元素没关系,是l[j]导致l数组越界了

好像是j的范围越界了,你把25098改小试一下呢?


从几次回复交流来看,应该是l的数据结构不支持用 l[j]形式访问,造成越界访问错误。

需要认真分析l的数据结构,找到其期望组合为 新m的数据结构时正确的对应关系,但现在给出的信息还不足以完成这样的信息提取。

 for j in range(0, 25098):  #25098
   if(ZZ[j]>=1 and YY[j]>=1):
       l= np.dstack((XX[j], YY[j], ZZ[j], UU[j]))
       if(len(l)>=j):#如果l长度超过j,则加入
         m.append(l[j])

先判断这个
l[j]
是否为空值,空的就不添加了

这么改如果还是一样说明你的这几个数组长度不一样

for j in range(0, len(XX)):  #25098
   if(ZZ[j]>=1 and YY[j]>=1):
       l= np.dstack((XX[j], YY[j], ZZ[j], UU[j]))
       m.append(l[j])