MATLAB: Maximum variable size allowed by the program is exceeded.

maximum variable size allowed by the program is exceeded.

Hi all I am applying a alogorithm after computing the gradient I am getting an error.My code is
if K>1
maxgrad=sqrt(double(gradscalex.*gradscalex+gradscaley.*gradscaley));
[gradscale gidx]=max(maxgrad,[],3);
gxtemp=zeros(M,N);
gytemp=gxtemp;
for kn=1:K
[rowidx colidx ]=ind2sub(size(gidx),find(gidx==kn));
gxtemp(rowidx,colidx)=gradscalex(rowidx,colidx,kn);
gytemp(rowidx,colidx)=gradscaley(rowidx,colidx,kn);
end
gradscalex=gxtemp;
gradscaley=gytemp;
end
??? Maximum variable size allowed by the program is exceeded.
Error in ==> Untitled at 36 gxtemp(rowidx,colidx)=gradscalex(rowidx,colidx,kn); Help me plz to solve this problem.I searched out but no solution.I am using Matlab version 7.11.0(R2010b) with 32-Bit operating system and 4GB RAM.

Best Answer

I have the vague impression that you're trying to do this:
[M,N,P]=size(gradscalex);
[mm,nn]=ndgrid(1:M,1:N); %pre-compute once!!
maxgrad=sqrt(double(gradscalex.*gradscalex+gradscaley.*gradscaley));
[~, gidx]=max(maxgrad,[],3);
idx=sub2ind([M,N,P],mm(:),nn(:),gidx(:));
gxtemp=reshape( gradscalex(idx) ,[M,N]);
gytemp=reshape( gradscaley(idx) ,[M,N]);