I would like your help to see whether there is a solution to the following "out of memory" problem in Matlab.
I have two big matrices in Matlab
A
of sizeax2
witha=7*10^4
B
of sizebx2
withb=7*10^4
I need to do a scatterplot of C
against D
obtained by doing
Ctemp=zeros(a,b);
for i=1:a
for j=1:b
Ctemp(i,j)=A(i,1)+B(j,1);
end
end
C=reshape(Ctemp, a*b,1);
C(C>=2 | C<=-2)=[];
Dtemp=zeros(a,b);
for i=1:a
for j=1:b
Dtemp(i,j)=A(i,2)+B(j,2);
end
end
D=reshape(Dtemp, a*b,1);
D(C>=2 | C<=-2)=[];
The problem is that Matlab gives out of memory error when I attempt to construct Ctemp
and Dtemp
. Is there any way to circumvent this issue or what I am trying to do is unfeasible?
A naive approach could be
C=[];
D=[];
for i=1:a
for j=1:b
if A(i,1)+B(j,1)<=2 && A(i,1)+B(j,1)>=-2
C=[C; A(i,1)+B(j,1)];
D=[D; A(i,2)+B(j,2)];
end
end
end
But I don't like this approach: it has a double loop that takes forever; it does not preallocate C,D
.
This seems to work better, I don't know why
C=cell(a,1);
D=cell(a,1);
for i=1:a
tempC=A(i,1)+B(:,1);
tempD=A(i,2)+B(:,2);
del=(A(i,1)+B(:,1)>2 | A(i,1)+B(:,1)<-2);
tempC(del)=[];
tempD(del)=[];
C{i}=tempC;
D{i}=tempD;
end