<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/247430</link>
    <title>MATLAB Central Newsreader - Indexing into large sparse matrix</title>
    <description>Feed for thread: Indexing into large sparse matrix</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 24 Mar 2009 20:44:02 -0400</pubDate>
      <title>Indexing into large sparse matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/247430#637391</link>
      <author>NIcholas </author>
      <description>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))&lt;br&gt;
&lt;br&gt;
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 &quot;maximum variable size allowed by the program is exceeded&quot;.  I was wondering if there was a way to extract these values without looping over the r, c index.&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
Nick</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 21:50:18 -0400</pubDate>
      <title>Re: Indexing into large sparse matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/247430#637407</link>
      <author>Matt </author>
      <description>&quot;NIcholas&quot; &amp;lt;remove.this_nmg33@cornell.edu&amp;gt; wrote in message &amp;lt;gqbgmi$ll6$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; 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))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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 &quot;maximum variable size allowed by the program is exceeded&quot;.  I was wondering if there was a way to extract these values without looping over the r, c index.&lt;br&gt;
&lt;br&gt;
In general, probably not, but there may be workarounds if you tell us how r,c were generated.&lt;br&gt;
&lt;br&gt;
In particular, do you have to index into A? Or could you perhaps index into nonzeros(A) ?</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 22:10:05 -0400</pubDate>
      <title>Re: Indexing into large sparse matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/247430#637409</link>
      <author>Bruno Luong</author>
      <description>&quot;NIcholas&quot; &amp;lt;remove.this_nmg33@cornell.edu&amp;gt; wrote in message &amp;lt;gqbgmi$ll6$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; 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))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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 &quot;maximum variable size allowed by the program is exceeded&quot;.  I was wondering if there was a way to extract these values without looping over the r, c index.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; Nick&lt;br&gt;
&lt;br&gt;
function v = spsubidx(S, r, c)&lt;br&gt;
if any(r(:)&amp;lt;1) || any(r(:)&amp;gt;size(S,1))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;error('Row subscript out of range')&lt;br&gt;
end&lt;br&gt;
if any(c(:)&amp;lt;1) || any(c(:)&amp;gt;size(S,2))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;error('Row subscript out of range')&lt;br&gt;
end&lt;br&gt;
[I J s]=find(S);&lt;br&gt;
[tf loc]= ismember([r(:) c(:)],[I J],'rows');&lt;br&gt;
v = zeros(size(tf));&lt;br&gt;
v(tf) = s(loc(tf));&lt;br&gt;
end % spsubidx&lt;br&gt;
&lt;br&gt;
% Command line&lt;br&gt;
A=sparse(10e6,10e6,pi,10e6,10e6)&lt;br&gt;
spsubidx(A,[1 1 size(A,1)],[1 1 size(A,2)])&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 22:17:01 -0400</pubDate>
      <title>Re: Indexing into large sparse matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/247430#637411</link>
      <author>Bruno Luong</author>
      <description>Sorry, the second error message should be:&lt;br&gt;
error('Column subscript out of range')&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 22:32:01 -0400</pubDate>
      <title>Re: Indexing into large sparse matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/247430#637416</link>
      <author>NIcholas </author>
      <description>The application is a &quot;sparse image&quot; 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.&lt;br&gt;
&lt;br&gt;
I planned to implement this as:&lt;br&gt;
[rr,cc,zz] = find(A);&lt;br&gt;
g = fspecial('gaussian', sz, sig);&lt;br&gt;
PDF=0;&lt;br&gt;
for i=1:size(g,1)&lt;br&gt;
for j=1:size(g,2)&lt;br&gt;
rNew = r - (sz(1)/2-1+i);&lt;br&gt;
cNew = c - (sz(2)/2-1+j);&lt;br&gt;
ind = sub2ind(size(A), rNew , cNew);&lt;br&gt;
PDF = PDF + g(i)*A(ind);&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
I wrote a simple mex function to extract the rNew,cNew elements from A (using the rr,cc index from find(A)).&lt;br&gt;
&lt;br&gt;
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: &quot;Indices are too large to convert to linear index&quot;.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;NIcholas&quot; &amp;lt;remove.this_nmg33@cornell.edu&amp;gt; wrote in message &amp;lt;gqbgmi$ll6$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; 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))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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 &quot;maximum variable size allowed by the program is exceeded&quot;.  I was wondering if there was a way to extract these values without looping over the r, c index.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; Nick</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 22:46:01 -0400</pubDate>
      <title>Re: Indexing into large sparse matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/247430#637418</link>
      <author>Matt </author>
      <description>&quot;NIcholas&quot; &amp;lt;remove.this_nmg33@cornell.edu&amp;gt; wrote in message &amp;lt;gqbn11$9c2$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; The application is a &quot;sparse image&quot; 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 - &lt;br&gt;
---------------------------&lt;br&gt;
&lt;br&gt;
If A represents the original image, then all this would require is&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt;PDF(A~=0)</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 22:48:02 -0400</pubDate>
      <title>Re: Indexing into large sparse matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/247430#637420</link>
      <author>Matt </author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;gqblnt$7c7$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; function v = spsubidx(S, r, c)&lt;br&gt;
&amp;gt; if any(r(:)&amp;lt;1) || any(r(:)&amp;gt;size(S,1))&lt;br&gt;
&amp;gt;     error('Row subscript out of range')&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; if any(c(:)&amp;lt;1) || any(c(:)&amp;gt;size(S,2))&lt;br&gt;
&amp;gt;     error('Row subscript out of range')&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; [I J s]=find(S);&lt;br&gt;
&amp;gt; [tf loc]= ismember([r(:) c(:)],[I J],'rows');&lt;br&gt;
&amp;gt; v = zeros(size(tf));&lt;br&gt;
&amp;gt; v(tf) = s(loc(tf));&lt;br&gt;
&amp;gt; end % spsubidx&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Command line&lt;br&gt;
&amp;gt; A=sparse(10e6,10e6,pi,10e6,10e6)&lt;br&gt;
&amp;gt; spsubidx(A,[1 1 size(A,1)],[1 1 size(A,2)])&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Bruno&lt;br&gt;
&lt;br&gt;
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...</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 22:55:18 -0400</pubDate>
      <title>Re: Indexing into large sparse matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/247430#637422</link>
      <author>Bruno Luong</author>
      <description>&quot;Matt &quot; &amp;lt;xys@whatever.com&amp;gt; wrote in message &amp;lt;gqbnv1$b9q$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Just as a side note, the above approach works only provided that you are trying to index exclusively into nonzero elements of S. &lt;br&gt;
&lt;br&gt;
No, it works for all cases, including indexing to zero.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 23:04:01 -0400</pubDate>
      <title>Re: Indexing into large sparse matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/247430#637425</link>
      <author>Matt </author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;gqbocm$a1e$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Matt &quot; &amp;lt;xys@whatever.com&amp;gt; wrote in message &amp;lt;gqbnv1$b9q$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Just as a side note, the above approach works only provided that you are trying to index exclusively into nonzero elements of S. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; No, it works for all cases, including indexing to zero.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
Alright, I see it now</description>
    </item>
  </channel>
</rss>

