Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Indexing into large sparse matrix
Date: Tue, 24 Mar 2009 22:32:01 +0000 (UTC)
Organization: Cornell University
Lines: 27
Message-ID: <gqbn11$9c2$1@fred.mathworks.com>
References: <gqbgmi$ll6$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1237933921 9602 172.30.248.38 (24 Mar 2009 22:32:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 24 Mar 2009 22:32:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1327593
Xref: news.mathworks.com comp.soft-sys.matlab:527423


The application is a "sparse image" filtering.  I have a large set of grid points with weights (intensities) and I am trying to filter this image with a gaussian and I am only interested in the value at non-zero locations in the original image - this arose as a discretization of a Kernel Density Estimation where I am only interested in the value of the PDF at the sample points - I was trying to make this quicker than O(N^2) and using a discrete grid makes it O(N*K) where K is the number of non-zero elements in the (truncated) Gaussian kernel.

I planned to implement this as:
[rr,cc,zz] = find(A);
g = fspecial('gaussian', sz, sig);
PDF=0;
for i=1:size(g,1)
for j=1:size(g,2)
rNew = r - (sz(1)/2-1+i);
cNew = c - (sz(2)/2-1+j);
ind = sub2ind(size(A), rNew , cNew);
PDF = PDF + g(i)*A(ind);
end
end

I wrote a simple mex function to extract the rNew,cNew elements from A (using the rr,cc index from find(A)).

I also tried A(sparse(rNew,cNew,true,size(A,1),size(B,2)))   - which is essentially what my mex function is computing  -  but I got a similar error: "Indices are too large to convert to linear index".


"NIcholas" <remove.this_nmg33@cornell.edu> wrote in message <gqbgmi$ll6$1@fred.mathworks.com>...
> I have a large sparse matrix with number of elements exceeding the maximum variable size allowed in matlab (though number of rows and columns do not) e.g. A = sparse([],[],[],10e6,10e6,0))
> 
> I have a list of row indices r, and colomn indices c (numel(r)==numel(c)).  I would like to extract the elements A(r(i),c(i)) for i=1:numel(r).  For a small matrix I could index as A(sub2ind(size(A),r,c)), however for large A I get the error that it can't index into A since "maximum variable size allowed by the program is exceeded".  I was wondering if there was a way to extract these values without looping over the r, c index.
> 
> Thanks,
> Nick