在 Matlab 中绘制散点图时的内存速度问题
Memory-speed issues when doing a scatter plot in Matlab
我在 Matlab 中遇到以下内存速度问题,希望您能帮助我了解是否有解决方案。
考虑以下 4
个大列向量 X1, X2, Y1, Y2
。
clear
rng default
P=10^8;
X1=rand(1,P)*5;
X2=rand(1,P)*5;
Y1=rand(1,P)*5;
Y2=rand(1,P)*5;
我想要做的是一个散点图,其中在 x 轴上我有 X1
和 X2
中任何可能的两个元素之间的总和,在 y 轴上我有Y1
和 Y2
中任何可能的两个元素之和。
我post 这里有三个我想过的选项都不行,主要是因为内存和速度问题。
选项 1(问题:执行循环时太慢,执行 vertcat
时内存不足)
Xtemp=cell(P,1);
Ytemp=cell(P,1);
for i=1:P
tic
Xtemp{i}=X1(i)+X2(:);
Ytemp{i}=Y1(i)+Y2(:);
toc
end
X=vertcat(Xtemp{:});
Y=vertcat(Ytemp{:});
scatter(X,Y)
选项 2(问题:执行循环时太慢,随着循环的进行时间增加,Matlab 变得疯狂并且即使我在之后停止循环也无法产生散点图5 次迭代)
for i=1:P
tic
scatter(X1(i)+X2(:), Y1(i)+Y2(:))
hold on
toc
end
选项 3(有点放弃)(问题:随着我增加 T
,散点越来越接近正确的正方形;我想知道尽管这是否是由于我使用 rand
生成数据而在选项 3 中我使用 randi
造成的;也许对于我的真实数据,散点图不会 "converge" 到真实图我加T
;还有,什么是"optimal"T
和R
?].
T=20;
R=500;
for t=1:T
tic
%select R points at random from X1,X2,Y1,Y2
X1sel=(X1(randi(R,R,1)));
X2sel=(X2(randi(R,R,1)));
Y1sel=(Y1(randi(R,R,1)));
Y2sel=(Y2(randi(R,R,1)));
%do option 1 among those points and plot
Xtempsel=cell(R,1);
Ytempsel=cell(R,1);
for r=1:R
Xtempsel{r}=X1sel(r)+X2sel(:);
Ytempsel{r}=Y1sel(r)+Y2sel(:);
end
Xsel=vertcat(Xtempsel{:});
Ysel=vertcat(Ytempsel{:});
scatter(Xsel,Ysel, 'b', 'filled')
hold on
toc
end
有没有办法做我想做的事或者根本不可能做的事?
您正在尝试构建一个包含 P^2 个元素(即 10^16)的向量。这比标准计算机的内存要大很多数量级(10GB 是 10^10 字节或 12 亿个双精度浮点数)。
对于较小的向量(即 P<1e4),尝试:
Xsum=bsxfun(@plus,X1,X2.'); %Matrix with the sum of any two elements from X1 and X2
X=X(:); %Reshape to vector
Ysum=bsxfun(@plus,Y1,Y2.');
Y=Y(:);
plot(X,Y,'.') %Plot as small dots, likely to take forever if there are too many points
要使用从这些大向量中随机选取的更合理数量的对来构建图形:
Npick=1e4;
sel1=randi(P,[Npick,1]);
sel2=randi(P,[Npick,1]);
Xsel=X1(sel1)+X2(sel2);
Ysel=Y1(sel1)+Y2(sel2);
plot(Xsel,Ysel,'.'); %Plot as small dots
我在 Matlab 中遇到以下内存速度问题,希望您能帮助我了解是否有解决方案。
考虑以下 4
个大列向量 X1, X2, Y1, Y2
。
clear
rng default
P=10^8;
X1=rand(1,P)*5;
X2=rand(1,P)*5;
Y1=rand(1,P)*5;
Y2=rand(1,P)*5;
我想要做的是一个散点图,其中在 x 轴上我有 X1
和 X2
中任何可能的两个元素之间的总和,在 y 轴上我有Y1
和 Y2
中任何可能的两个元素之和。
我post 这里有三个我想过的选项都不行,主要是因为内存和速度问题。
选项 1(问题:执行循环时太慢,执行 vertcat
时内存不足)
Xtemp=cell(P,1);
Ytemp=cell(P,1);
for i=1:P
tic
Xtemp{i}=X1(i)+X2(:);
Ytemp{i}=Y1(i)+Y2(:);
toc
end
X=vertcat(Xtemp{:});
Y=vertcat(Ytemp{:});
scatter(X,Y)
选项 2(问题:执行循环时太慢,随着循环的进行时间增加,Matlab 变得疯狂并且即使我在之后停止循环也无法产生散点图5 次迭代)
for i=1:P
tic
scatter(X1(i)+X2(:), Y1(i)+Y2(:))
hold on
toc
end
选项 3(有点放弃)(问题:随着我增加 T
,散点越来越接近正确的正方形;我想知道尽管这是否是由于我使用 rand
生成数据而在选项 3 中我使用 randi
造成的;也许对于我的真实数据,散点图不会 "converge" 到真实图我加T
;还有,什么是"optimal"T
和R
?].
T=20;
R=500;
for t=1:T
tic
%select R points at random from X1,X2,Y1,Y2
X1sel=(X1(randi(R,R,1)));
X2sel=(X2(randi(R,R,1)));
Y1sel=(Y1(randi(R,R,1)));
Y2sel=(Y2(randi(R,R,1)));
%do option 1 among those points and plot
Xtempsel=cell(R,1);
Ytempsel=cell(R,1);
for r=1:R
Xtempsel{r}=X1sel(r)+X2sel(:);
Ytempsel{r}=Y1sel(r)+Y2sel(:);
end
Xsel=vertcat(Xtempsel{:});
Ysel=vertcat(Ytempsel{:});
scatter(Xsel,Ysel, 'b', 'filled')
hold on
toc
end
有没有办法做我想做的事或者根本不可能做的事?
您正在尝试构建一个包含 P^2 个元素(即 10^16)的向量。这比标准计算机的内存要大很多数量级(10GB 是 10^10 字节或 12 亿个双精度浮点数)。
对于较小的向量(即 P<1e4),尝试:
Xsum=bsxfun(@plus,X1,X2.'); %Matrix with the sum of any two elements from X1 and X2
X=X(:); %Reshape to vector
Ysum=bsxfun(@plus,Y1,Y2.');
Y=Y(:);
plot(X,Y,'.') %Plot as small dots, likely to take forever if there are too many points
要使用从这些大向量中随机选取的更合理数量的对来构建图形:
Npick=1e4;
sel1=randi(P,[Npick,1]);
sel2=randi(P,[Npick,1]);
Xsel=X1(sel1)+X2(sel2);
Ysel=Y1(sel1)+Y2(sel2);
plot(Xsel,Ysel,'.'); %Plot as small dots