spdiags grinds to a halt!

4 views (last 30 days)
Mikhail  Kandel
Mikhail Kandel on 3 Jan 2014
Edited: Mikhail Kandel on 29 Jan 2014
I'm trying to insert 441 diagonals into a sparse matrix that is 614292x614292 in size. My matrix vaguely a Toeplitz matrix and I have something like 32 GB of ram.
After inserting about 150 diagonals the inserting slows to a halt.
Looking at spdiags it appears to insert each diagonal seperately and perform some kind of 'compression', I'm wondering if there is some way to insert them all at once.
function S=buildconvmatrix(Cb,m,n)
%In the future have custom diagonals...
[cm,cn]=size(Cb);
[Xi,Yi]=meshgrid(1:cm,1:cn);
Xi=reshape(Xi,[],1);
Yi=reshape(Yi,[],1);
zpoint = ceil(numel(Cb)/2);
S = sparse(m*n,m*n);
for q=1:numel(Xi)
row(q) = q-zpoint;
end
vList = reshape(Cb,1,[]);
S=spdiags(ones(m*n,1)*vList,row,S);% Hours
end
--Edit--
For example this is much faster, which leads to me believe that there is some kind of performance bug in Matlab's stock implementation.
function S=buildconvmatrixfaster(Cb,m,n)
[cm,cn]=size(Cb);
[Xi,Yi]=meshgrid(1:cm,1:cn);
Xi=reshape(Xi,[],1);
Yi=reshape(Yi,[],1);
zpoint = ceil(numel(Cb)/2);
cuts = 12;
M=cell(cuts,1);
for i=1:cuts
M{i}=sparse(m*n,m*n);
end
els=numel(Xi);
bin=els/cuts;
for q=1:els
disp(strcat('On:',num2str(q),'/',num2str(numel(Xi))));
v=Cb(Xi(q),Yi(q));
row = q-zpoint;
foo=ceil(q/bin);
M{foo}=spdiags(ones(m*n,1)*v,row,M{foo});
end
S=sparse(m*n,m*n);
disp('Adding');
for i=1:cuts
S=S+M{i};
end
end
  3 Comments
Matt J
Matt J on 3 Jan 2014
What are the dimensions of Cb?
Mikhail  Kandel
Mikhail Kandel on 3 Jan 2014
Edited: Mikhail Kandel on 3 Jan 2014
@Matt J its 21x21

Sign in to comment.

Answers (2)

Matt J
Matt J on 3 Jan 2014
Instead of
S = sparse(m*n,m*n);
try
S = spalloc(m*n,m*n, numel(ones(m*n,1)*vList));

Matt J
Matt J on 3 Jan 2014
  1 Comment
Mikhail  Kandel
Mikhail Kandel on 3 Jan 2014
This might be close, I'm trying to check a C++ program where I hit each pixel of an image with a varying convolution kernel. I'm writing it in matlab to get the more natural mathematical form, so I can debug if my math is wrong or if my C++ is wrong.

Sign in to comment.

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!