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:48:02 +0000 (UTC)
Organization: Xoran Technologies
Lines: 22
Message-ID: <gqbnv1$b9q$1@fred.mathworks.com>
References: <gqbgmi$ll6$1@fred.mathworks.com> <gqblnt$7c7$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 1237934882 11578 172.30.248.38 (24 Mar 2009 22:48:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 24 Mar 2009 22:48:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:527427


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gqblnt$7c7$1@fred.mathworks.com>...
>
> 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

Just as a side note, the above approach works only provided that you are trying to index exclusively into nonzero elements of S. That does happen to be what the OP wants, but we only found that out afterward...