在八度音阶上向量化多维数组填充
Vectorize multi-dimensional array population on octave
这是我的实际代码:
Lr = linspace(100,300,10);
vi = linspace(10,30,10);
vf = linspace(10,30,10);
b = linspace(0.5,1.2,10);
h = linspace(0.3,0.8,10);
Rc = [1000, 1500, 2000, 2500, 3000, 3500;
29, 22.4, 17.3, 13.4, 10.4, 8];
rti = randi(5, 10, 1, 10, 10, 10, 10);
for kk = 1:rows(Lr)
for jj = 1:length(vi)
for ll = 1:length(vf)
for mm = 1:length(b)
for nn = 1:length(h)
ratt(kk,1,jj,ll,mm,nn) = Rc(2,rti(kk,1,jj,ll,mm,nn));
endfor
endfor
endfor
endfor
endfor
在这个例子中,所有向量的长度都是10,但在实际代码中,它们可以是从1到100。
我怎样才能删除所有这些循环?
- 从
Rc
获取 ratt
的所有值作为一维数组。
- 将
ratt
重塑为原始代码中的 6 维数组。
代码:
ratt = Rc(2, rti(1:rows(Lr), 1, 1:length(vi), 1:length(vf), 1:length(b), 1:length(h)));
ratt = reshape(ratt, [rows(Lr), 1, length(vi), length(vf), length(b), length(h)]);
测试: 为了测试矢量化代码和 non-vectorized 代码的相等性和速度,我编写了以下代码:
Lr = linspace(100,300,10);
vi = linspace(10,30,10);
vf = linspace(10,30,10);
b = linspace(0.5,1.2,10);
h = linspace(0.3,0.8,10);
Rc = [1000, 1500, 2000, 2500, 3000, 3500;
29, 22.4, 17.3, 13.4, 10.4, 8];
rti = randi(5, 10, 1, 10, 10, 10, 10);
disp('Non-vectorized:')
tic
for kk = 1:rows(Lr)
for jj = 1:length(vi)
for ll = 1:length(vf)
for mm = 1:length(b)
for nn = 1:length(h)
ratt(kk,1,jj,ll,mm,nn) = Rc(2,rti(kk,1,jj,ll,mm,nn));
endfor
endfor
endfor
endfor
endfor
toc
disp('')
disp('Vectorized:')
tic
ratt1 = Rc(2, rti(1:rows(Lr), 1, 1:length(vi), 1:length(vf), 1:length(b), 1:length(h)));
ratt1 = reshape(ratt1, [rows(Lr), 1, length(vi), length(vf), length(b), length(h)]);
toc
disp('')
if(isequal(ratt,ratt1))
disp('ratt is equal to ratt1!')
else
disp('ratt is not equal to ratt1!')
endif
输出:
Non-vectorized:
Elapsed time is 0.16697 seconds.
Vectorized:
Elapsed time is 0.000309944 seconds.
ratt is equal to ratt1!
这是我的实际代码:
Lr = linspace(100,300,10);
vi = linspace(10,30,10);
vf = linspace(10,30,10);
b = linspace(0.5,1.2,10);
h = linspace(0.3,0.8,10);
Rc = [1000, 1500, 2000, 2500, 3000, 3500;
29, 22.4, 17.3, 13.4, 10.4, 8];
rti = randi(5, 10, 1, 10, 10, 10, 10);
for kk = 1:rows(Lr)
for jj = 1:length(vi)
for ll = 1:length(vf)
for mm = 1:length(b)
for nn = 1:length(h)
ratt(kk,1,jj,ll,mm,nn) = Rc(2,rti(kk,1,jj,ll,mm,nn));
endfor
endfor
endfor
endfor
endfor
在这个例子中,所有向量的长度都是10,但在实际代码中,它们可以是从1到100。 我怎样才能删除所有这些循环?
- 从
Rc
获取ratt
的所有值作为一维数组。 - 将
ratt
重塑为原始代码中的 6 维数组。
代码:
ratt = Rc(2, rti(1:rows(Lr), 1, 1:length(vi), 1:length(vf), 1:length(b), 1:length(h)));
ratt = reshape(ratt, [rows(Lr), 1, length(vi), length(vf), length(b), length(h)]);
测试: 为了测试矢量化代码和 non-vectorized 代码的相等性和速度,我编写了以下代码:
Lr = linspace(100,300,10);
vi = linspace(10,30,10);
vf = linspace(10,30,10);
b = linspace(0.5,1.2,10);
h = linspace(0.3,0.8,10);
Rc = [1000, 1500, 2000, 2500, 3000, 3500;
29, 22.4, 17.3, 13.4, 10.4, 8];
rti = randi(5, 10, 1, 10, 10, 10, 10);
disp('Non-vectorized:')
tic
for kk = 1:rows(Lr)
for jj = 1:length(vi)
for ll = 1:length(vf)
for mm = 1:length(b)
for nn = 1:length(h)
ratt(kk,1,jj,ll,mm,nn) = Rc(2,rti(kk,1,jj,ll,mm,nn));
endfor
endfor
endfor
endfor
endfor
toc
disp('')
disp('Vectorized:')
tic
ratt1 = Rc(2, rti(1:rows(Lr), 1, 1:length(vi), 1:length(vf), 1:length(b), 1:length(h)));
ratt1 = reshape(ratt1, [rows(Lr), 1, length(vi), length(vf), length(b), length(h)]);
toc
disp('')
if(isequal(ratt,ratt1))
disp('ratt is equal to ratt1!')
else
disp('ratt is not equal to ratt1!')
endif
输出:
Non-vectorized:
Elapsed time is 0.16697 seconds.
Vectorized:
Elapsed time is 0.000309944 seconds.
ratt is equal to ratt1!