Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Indexing into large sparse matrix
Date: Tue, 24 Mar 2009 22:10:05 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 26
Message-ID: <gqblnt$7c7$1@fred.mathworks.com>
References: <gqbgmi$ll6$1@fred.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1237932605 7559 172.30.248.37 (24 Mar 2009 22:10:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 24 Mar 2009 22:10:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:527416


"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

function v = spsubidx(S, r, c)
if any(r(:)<1) || any(r(:)>size(S,1))
    error('Row subscript out of range')
end
if any(c(:)<1) || any(c(:)>size(S,2))
    error('Row subscript out of range')
end
[I J s]=find(S);
[tf loc]= ismember([r(:) c(:)],[I J],'rows');
v = zeros(size(tf));
v(tf) = s(loc(tf));
end % spsubidx

% Command line
A=sparse(10e6,10e6,pi,10e6,10e6)
spsubidx(A,[1 1 size(A,1)],[1 1 size(A,2)])

% Bruno