Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: creating a sparse matrix from a file

Subject: creating a sparse matrix from a file

From: akshay bhat

Date: 04 May, 2008 08:22:28

Message: 1 of 4

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?

Subject: Re: creating a sparse matrix from a file

From: Tim Davis

Date: 04 May, 2008 20:33:03

Message: 2 of 4

akshay bhat <akshayubhat@gmail.com> wrote in message
<2b89e1a3-a4f7-47ae-b8e0-571a8aa640d1@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.

Subject: Re: creating a sparse matrix from a file

From: akshay bhat

Date: 05 May, 2008 23:18:04

Message: 3 of 4

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

Subject: Re: creating a sparse matrix from a file

From: us

Date: 05 May, 2008 23:34:03

Message: 4 of 4

akshay bhat:
<SNIP sparse mat from a list of indices...

> 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;

a hint:

     help spconvert;

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
spconvert us 05 May, 2008 19:35:18
reference us 05 May, 2008 19:35:17
sparse Tim Davis 04 May, 2008 16:35:13
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics