Path: news.mathworks.com!newsfeed-00.mathworks.com!newscon02.news.prodigy.net!prodigy.net!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!s33g2000pri.googlegroups.com!not-for-mail
From: akshay bhat <akshayubhat@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: creating a sparse matrix from a file
Date: Mon, 5 May 2008 16:18:04 -0700 (PDT)
Organization: http://groups.google.com
Lines: 48
Message-ID: <8629a167-ade8-41e3-89cc-f558833a4f23@s33g2000pri.googlegroups.com>
References: <2b89e1a3-a4f7-47ae-b8e0-571a8aa640d1@h1g2000prh.googlegroups.com> 
NNTP-Posting-Host: 59.182.125.28
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Trace: posting.google.com 1210029484 26471 127.0.0.1 (5 May 2008 23:18:04 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 5 May 2008 23:18:04 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: s33g2000pri.googlegroups.com; posting-host=59.182.125.28; 
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) 
Bytes: 2445
Xref: news.mathworks.com comp.soft-sys.matlab:466799


On May 5, 1:33 am, "Tim Davis" <da...@cise.ufl.edu> wrote:
> akshay bhat <akshayub...@gmail.com> wrote in message
>
> <2b89e1a3-a4f7-47ae-b8e0-571a8aa64...@h1g2000prh.googlegroups.com>...
>
>
>
> > i have a list in following form (in a csv file)
> > 12,54
> > 3,4
> > .
> > .
> > .
> > i trying to create a sparse matrix by using following code
>
> > smat(12,54)=1;
>
> > now the size of the list is around 1 million that is there
> are around
> > 1million pairs.
> > a simple for loop is taking 30 minutes for 10 percent of
> of values.
> > is there any shortcut way for this.
> > to speed up the process?
>
> Save the file in space or tab-delimited form, without the
> commas.  Then do
>
> load smat.txt
> A = sparse(smat(:,1),smat(:,2),1) ;
>
> The code is slow because you are abusing subsref (the
> smat(i,j)=1 statement).
>
> If you can't remove the commas, do:
>
> f=fopen('smat.csv') ;
> t = fscanf (f,'%d, %d') ;
> fclose(f) ;
> t = reshape (t, 2, length(t)/2)' ;
> A = sparse (t(:,1), t(:,2), 1) ;
>
> See Loren's March 1st, 2007 blog for more details.
>
> Astute readers might know of something better than the
> reshape I'm using.

hey thanks a lot for help