我想通过向量元素的适当直方图(长度 system_size 的 num_samples 个样本)和相应的聚类函数 T2、T3 来计算向量样本的 2 点和 3 点相关函数 R2、R3。为简单起见,我正在考虑跨统一箱进行直方图。
什么是矢量化和/或加速以下代码的好方法?
n = length(mesh);
R2 = zeros(n, n);
R3 = zeros(n, n, n);
for sample_id=1:num_samples
s = samples(:, sample_id);
d = mesh(2) - mesh(1);
% Which bin does the ith sample s belong to?
bins = ceil((s - mesh(1))/d);
% Compute two-point correlation function
for i = 1:system_size
for j = 1:system_size
if i ~= j
R2(bins(i), bins(j))=R2(bins(i), bins(j))+1;
end
end
end
% Compute three-point correlation function
for i = 1:system_size
for j = 1:system_size
if i ~= j
for k = 1:system_size
if k ~= j && k ~= i
R3(bins(i), bins(j), bins(k))=R3(bins(i), bins(j), bins(k))+1;
T3(x1, x2, x3) = R3(x1,x2,x3)-R1(x1)*R2(x2,x3)-R1(x2)*R2(x1,x3)...
-R1(x3)*R2(x1,x2)+2*R1(x1)*R1(x2)*R1(x3);
end
end
end
end
end
end
R2 = R2/sum(R2(:));
R3 = R3/sum(R3(:));
T3 = zeros(n, n, n);
% Compute three-point cluster function
for i = 1:n
for j = 1:n
if i ~= j
for k = 1:n
if k ~= j && k ~= i
T3(x1, x2, x3) = R3(x1,x2,x3)-R1(x1)*R2(x2,x3)-R1(x2)*R2(x1,x3)...
-R1(x3)*R2(x1,x2)+2*R1(x1)*R1(x2)*R1(x3);
end
end
end
end
end
我天真地认为 hist3(bins, bins...) 或 crosstab(bins, bins) 几乎可以做我想要的,即寻找向量元素的相关出现,但事实并非如此。
例子:
如果我在最外层循环中的输入是
s = [1.2 3.1 4.6 4.7 5.1]
mesh = 0:0.5:6
那么量化的数据应该是
bins = [3 7 10 10 11]
和 R2 应该是
>> R2
R2 =
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 2 1 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 2 1 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 0 2 0 0 2 2 0
0 0 1 0 0 0 1 0 0 2 0 0
0 0 0 0 0 0 0 0 0 0 0 0