简体   繁体   English

Python & Numpy - 将每个单元格为 20x20 数组的 10x10 数组重塑为 10x10x20x20

[英]Python & Numpy - reshape 10x10 array where each cell is 20x20 array into a 10x10x20x20

I have kind a of weird problem where I have a .mat file that comes in aa 10x10 array where each index is a 20x20 array representing a gray image.我有一个奇怪的问题,我有一个 .mat 文件,它来自一个 10x10 数组,其中每个索引是一个 20x20 数组,表示一个灰色图像。 So I have 10 trials and each trial are a time series with 10 intervals and each interval is tracked as a 20x20 image.所以我有 10 次试验,每次试验都是一个具有 10 个间隔的时间序列,每个间隔都作为 20x20 图像进行跟踪。 In order to run tensor regression on this using tensorly this needs to be formatted as a (trails, time series, x-dim, y-dim) tensor.为了使用张量运行张量回归,需要将其格式化为(轨迹、时间序列、x-dim、y-dim)张量。 Is there a clean way to do this?有没有干净的方法来做到这一点? I tried np.reshape on the mat file but that won't work cause the mat file dimensions are 10x10, cause like I said each cell of the array is the 20x20.我在 mat 文件上尝试了 np.reshape 但这不起作用,因为 mat 文件的尺寸是 10x10,因为就像我说的数组的每个单元格都是 20x20。

So for example let's say the mat file is variable name 'mat_var.'例如,假设 mat 文件是变量名称“mat_var”。 Then mat_var[0,0] will be a 20x20 array and mat_var will be a 10x10 array那么 mat_var[0,0] 将是一个 20x20 的数组,而 mat_var 将是一个 10x10 的数组

Goal is to reshape a 10x10 array where each index is a 20x20 array into a 10x10x20x20 array.目标是将每个索引为 20x20 数组的 10x10 数组重塑为 10x10x20x20 数组。

so apparent if you do tensor = np.full((10,10,20,20),0) then you can set each cell accordingly like很明显,如果你做 tensor = np.full((10,10,20,20),0) 那么你可以相应地设置每个单元格

tensor[x,y] = mat[x,y]张量[x,y] = mat[x,y]

and it'll just fill the 20x20 if the size of the array is also 20x20如果数组的大小也是 20x20,它只会填充 20x20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM