Path: news.mathworks.com!not-for-mail From: "Bruno Luong" <b.luong@fogale.findmycountry> Newsgroups: comp.soft-sys.matlab Subject: Re: Index matrix to sparse matrix... saving memory Date: Sat, 29 May 2010 16:43:05 +0000 (UTC) Organization: FOGALE nanotech Lines: 25 Message-ID: <htrg6p$2h3$1@fred.mathworks.com> References: <htrfev$fo7$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=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1275151385 2595 172.30.248.37 (29 May 2010 16:43:05 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Sat, 29 May 2010 16:43:05 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 390839 Xref: news.mathworks.com comp.soft-sys.matlab:640489 "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!! > > S_s = sparse(S);%<-- Much less memory (in the above case there isn't much savings) > > Is there any way address S_s row wise to grab out the vectors I need without turning it back into a full matrix? The following command will return the non-zeros elements of matrix: nonzeros(S_s(row,:)) I strongly recommend to transpose your matrix so that to grab the index by column Due to the way sparse matrix organized in the computer memory, access by column is much faster. AFAICS, you don not need my sparse access tool. Bruno