将数组与交替列选择相乘
Multiplying arrays with alternating column selection
我有一个文件 testforce.dat
,它显示了分为 9 列和 3 行的值。前3列代表:
p1 p2 p3 f1 f2 f3 r1 r2 r3
18 5 27 20 21 8 14 12 25
9 26 23 1 4 10 7 16 24
19 22 15 13 17 6 11 2 3
我有100个这种时装的档案。
我现在想为文件 force_00000.dat
计算向量 g = [sum(p1*f1), sum(p2*f2), sum(p3*f3)]
但对于下一个文件 force_00001.dat
向量应该使用其他列 h = [sum(p1*r1), sum(p2*r2), sum(p3*r3)]
.
目前我正在使用 glob 函数将我的文件读入数组。它将每一行放入一个数组中。
我不确定如何完成我的交替数组乘法,如果有任何建议,我将不胜感激:)
import numpy as np
import glob
i = 100
for x in range(0,int(i)):
## turns x into a string and adds if necessary "0" to achieve a fixed digit number;
y = str(x).zfill(5)
## the structure of the forcefile is "force_[00000-00099]";
files = sorted(glob.glob('.//results/force/force_%s.dat' % y))
column_names=('#position')
print files
## loads the file data into arrays
arrays=[np.loadtxt(filename) for filename in files]
print arrays
编辑:我测试了第一个文件的负载:
b=np.array(arrays)
print b.shape
然后我得到 (1,3,9) 作为我生成的数组的形状。
Edit2:我想到了使用 "usecols" 然后乘以所需的值:
xposition=[np.loadtxt(filename,usecols= (0,1,2)) for filename in files]
xforce1=[np.loadtxt(filename,usecols= (3,4,5)) for filename in files]
print xposition
print xforce1
xp=np.asarray(xposition)
xf1=np.asarray(xforce1)
print xp
g=np.multiply(xp,xf1)
print g
这生成了以下输出:
[[[ 360. 105. 216.]
[ 9. 104. 230.]
[ 247. 374. 90.]]]
这意味着我有(p11 和 f11 是第一行的值,p21 来自第二行...)
[[[p11*f11 p12*f12 p13*f13]
[p21*f21 p22*f22 p23*f23]
[p31*f31 p32*f32 p33*f33]]]
看来我至少完成了一个文件。所需的 g(g1,g2,g3) 应如下所示:
p11*f11+p21*f21+p31*f31= g1
p12*f12+p22*f22+p32*f32= g2
p13*f13+p23*f23+p33*f33= g3
抱歉,如果这是一个完全新手的问题,但我还不太熟悉 python :)
对于交替值的问题,我正在考虑使用一个 if 函数来检查循环中的 "i" 是否为偶数
loadtxt
returns 一个数组。 [loadtxt(name) for name in filenames]
生成一个数组列表,每个名称一个数组。 np.array([...])
从该列表中生成一个数组。如果各个数组的大小都相同,则生成的数组将为 3d。
如果您需要区别对待所有其他文件,您可以通过索引将它们作为一个集合访问
arr[::2,...]
arr[1;:2,...]
要将示例文件中的 2 组列相乘:
In [558]: txt=b"""p1 p2 p3 f1 f2 f3 r1 r2 r3
...: 18 5 27 20 21 8 14 12 25
...: 9 26 23 1 4 10 7 16 24
...: 19 22 15 13 17 6 11 2 3"""
In [560]: arr = np.loadtxt(txt.splitlines(),skiprows=1,dtype=int)
In [561]: arr
Out[561]:
array([[18, 5, 27, 20, 21, 8, 14, 12, 25],
[ 9, 26, 23, 1, 4, 10, 7, 16, 24],
[19, 22, 15, 13, 17, 6, 11, 2, 3]])
In [562]: arr[:, 0:3]*arr[:, 3:6]
Out[562]:
array([[360, 105, 216],
[ 9, 104, 230],
[247, 374, 90]])
In [563]: arr[:, 0:3]*arr[:, 6:9]
Out[563]:
array([[252, 60, 675],
[ 63, 416, 552],
[209, 44, 45]])
如果arr
是加载多个文件的 3d 数组,
arr1 = arr[::2,...]
arr2 = arr[1::2,...]
arr1[:,:,0:3] * arr1[:,:,3:6]
etc
我有一个文件 testforce.dat
,它显示了分为 9 列和 3 行的值。前3列代表:
p1 p2 p3 f1 f2 f3 r1 r2 r3
18 5 27 20 21 8 14 12 25
9 26 23 1 4 10 7 16 24
19 22 15 13 17 6 11 2 3
我有100个这种时装的档案。
我现在想为文件 force_00000.dat
计算向量 g = [sum(p1*f1), sum(p2*f2), sum(p3*f3)]
但对于下一个文件 force_00001.dat
向量应该使用其他列 h = [sum(p1*r1), sum(p2*r2), sum(p3*r3)]
.
目前我正在使用 glob 函数将我的文件读入数组。它将每一行放入一个数组中。 我不确定如何完成我的交替数组乘法,如果有任何建议,我将不胜感激:)
import numpy as np
import glob
i = 100
for x in range(0,int(i)):
## turns x into a string and adds if necessary "0" to achieve a fixed digit number;
y = str(x).zfill(5)
## the structure of the forcefile is "force_[00000-00099]";
files = sorted(glob.glob('.//results/force/force_%s.dat' % y))
column_names=('#position')
print files
## loads the file data into arrays
arrays=[np.loadtxt(filename) for filename in files]
print arrays
编辑:我测试了第一个文件的负载:
b=np.array(arrays)
print b.shape
然后我得到 (1,3,9) 作为我生成的数组的形状。
Edit2:我想到了使用 "usecols" 然后乘以所需的值:
xposition=[np.loadtxt(filename,usecols= (0,1,2)) for filename in files]
xforce1=[np.loadtxt(filename,usecols= (3,4,5)) for filename in files]
print xposition
print xforce1
xp=np.asarray(xposition)
xf1=np.asarray(xforce1)
print xp
g=np.multiply(xp,xf1)
print g
这生成了以下输出:
[[[ 360. 105. 216.]
[ 9. 104. 230.]
[ 247. 374. 90.]]]
这意味着我有(p11 和 f11 是第一行的值,p21 来自第二行...)
[[[p11*f11 p12*f12 p13*f13]
[p21*f21 p22*f22 p23*f23]
[p31*f31 p32*f32 p33*f33]]]
看来我至少完成了一个文件。所需的 g(g1,g2,g3) 应如下所示:
p11*f11+p21*f21+p31*f31= g1
p12*f12+p22*f22+p32*f32= g2
p13*f13+p23*f23+p33*f33= g3
抱歉,如果这是一个完全新手的问题,但我还不太熟悉 python :)
对于交替值的问题,我正在考虑使用一个 if 函数来检查循环中的 "i" 是否为偶数
loadtxt
returns 一个数组。 [loadtxt(name) for name in filenames]
生成一个数组列表,每个名称一个数组。 np.array([...])
从该列表中生成一个数组。如果各个数组的大小都相同,则生成的数组将为 3d。
如果您需要区别对待所有其他文件,您可以通过索引将它们作为一个集合访问
arr[::2,...]
arr[1;:2,...]
要将示例文件中的 2 组列相乘:
In [558]: txt=b"""p1 p2 p3 f1 f2 f3 r1 r2 r3
...: 18 5 27 20 21 8 14 12 25
...: 9 26 23 1 4 10 7 16 24
...: 19 22 15 13 17 6 11 2 3"""
In [560]: arr = np.loadtxt(txt.splitlines(),skiprows=1,dtype=int)
In [561]: arr
Out[561]:
array([[18, 5, 27, 20, 21, 8, 14, 12, 25],
[ 9, 26, 23, 1, 4, 10, 7, 16, 24],
[19, 22, 15, 13, 17, 6, 11, 2, 3]])
In [562]: arr[:, 0:3]*arr[:, 3:6]
Out[562]:
array([[360, 105, 216],
[ 9, 104, 230],
[247, 374, 90]])
In [563]: arr[:, 0:3]*arr[:, 6:9]
Out[563]:
array([[252, 60, 675],
[ 63, 416, 552],
[209, 44, 45]])
如果arr
是加载多个文件的 3d 数组,
arr1 = arr[::2,...]
arr2 = arr[1::2,...]
arr1[:,:,0:3] * arr1[:,:,3:6]
etc