Path: news.mathworks.com!not-for-mail From: "Anthony Hopf" <anthony.hopf@gmail.com> Newsgroups: comp.soft-sys.matlab Subject: Re: Index matrix to sparse matrix... saving memory Date: Sat, 29 May 2010 18:10:21 +0000 (UTC) Organization: The MathWorks, Inc. Lines: 23 Message-ID: <htrlad$in3$1@fred.mathworks.com> References: <htrfev$fo7$1@fred.mathworks.com> <htrk32$2vm$1@fred.mathworks.com> Reply-To: "Anthony Hopf" <anthony.hopf@gmail.com> NNTP-Posting-Host: webapp-03-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1275156621 19171 172.30.248.38 (29 May 2010 18:10:21 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Sat, 29 May 2010 18:10:21 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1899184 Xref: news.mathworks.com comp.soft-sys.matlab:640510 "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <htrk32$2vm$1@fred.mathworks.com>... > "Anthony Hopf" <anthony.hopf@gmail.com> wrote in message <htrfev$fo7$1@fred.mathworks.com>... > > Latest problem.... > > > > I have a matrix NxM that is N indexing vectors. Some of these vectors have very few index values, but they all have to be M long (the largest number of index values) so they are filled with zeros. Turns out I only utilize about 3% of the matrix to valid index values so I thought it would be a "good" idea to take advantage of sparse matrices to save a ton of memory, the MxN matrix can be over a gig. > > > > S = [1 2 3 0 0 0 0 0 0 > > 4 5 6 0 0 0 0 0 0 > > 7 8 9 10 11 12 13 14 0 > > 15 16 17 18 19 20 21 22 23] > > > > I would access each vector by S(1,:) and would have to cull out the 0's... but it would be great to just get the nonzeros!! > ================== > > Why not construct S as sparse logical with each row of S (or column as Bruno recommended), consisting of logical indices instead of linear ones? > > That way the sparse matrix would consume even less memory (because it's type logical instead of double). Plus, you wouldn't have to sift out the zeros when indexing, because sparse logical indexing already does that for you under the hood. Matt, that is a very good point. but I use the index values to pick out points in a 3d xyz data structure, add them and replace the value in the 3d xyz matrix, these index values, as noted above, are not uniform from spot to spot (I am simulating a beam sampling space so the number of valid points increase with range from the sensor). I'll think about it, maybe I can apply this technique... Thanks again! Anthony