Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Creating Adjacency Matrix from a dataset

Subject: Creating Adjacency Matrix from a dataset

From: Tehmina

Date: 9 Apr, 2013 07:28:11

Message: 1 of 8

Hi,
i am a new user of matlab and need help in creating adjacency matrix from a data set. the dataset actulally is a real world dataset of co-authorship relationships. it looks like the following:
from node id to node id
0 1
0 2
0 5
1 2
1 3
1 4
2 3
2 5
3 1
3 4
3 5
4 0
4 2
5 2
5 4

the adjacency matrix for above will be
M= 0 1 1 0 0 1
      0 0 1 1 1 0
      0 0 0 1 0 1
      0 1 0 0 1 1
      1 0 1 0 0 0
      0 0 1 0 1 0

i need code to perform the above task in matlab
Can anyone help plz

Subject: Creating Adjacency Matrix from a dataset

From: kinor

Date: 9 Apr, 2013 09:09:09

Message: 2 of 8

"Tehmina" wrote in message <kk0fub$gji$1@newscl01ah.mathworks.com>...
> Hi,
> i am a new user of matlab and need help in creating adjacency matrix from a data set. the dataset actulally is a real world dataset of co-authorship relationships. it looks like the following:
> from node id to node id
> 0 1
> 0 2
> 0 5
> 1 2
> 1 3
> 1 4
> 2 3
> 2 5
> 3 1
> 3 4
> 3 5
> 4 0
> 4 2
> 5 2
> 5 4
>
> the adjacency matrix for above will be
> M= 0 1 1 0 0 1
> 0 0 1 1 1 0
> 0 0 0 1 0 1
> 0 1 0 0 1 1
> 1 0 1 0 0 0
> 0 0 1 0 1 0
>
> i need code to perform the above task in matlab
> Can anyone help plz

Hi Tehmina

looks like homework, some hints:

indexing starts at 1 in Matlab. so add 1 to your node array before continuing
help sub2ind

hth
kinor

Subject: Creating Adjacency Matrix from a dataset

From: Bruno Luong

Date: 9 Apr, 2013 09:31:12

Message: 3 of 8

 A=[0 1
0 2
0 5
1 2
1 3
1 4
2 3
2 5
3 1
3 4
3 5
4 0
4 2
5 2
5 4];

>> accumarray(A+1,1)

ans =

     0 1 1 0 0 1
     0 0 1 1 1 0
     0 0 0 1 0 1
     0 1 0 0 1 1
     1 0 1 0 0 0
     0 0 1 0 1 0

% Bruno

Subject: Creating Adjacency Matrix from a dataset

From: Tehmina

Date: 10 Apr, 2013 07:16:05

Message: 4 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kk0n50$639$1@newscl01ah.mathworks.com>...
> A=[0 1
> 0 2
> 0 5
> 1 2
> 1 3
> 1 4
> 2 3
> 2 5
> 3 1
> 3 4
> 3 5
> 4 0
> 4 2
> 5 2
> 5 4];
>
> >> accumarray(A+1,1)
>
> ans =
>
> 0 1 1 0 0 1
> 0 0 1 1 1 0
> 0 0 0 1 0 1
> 0 1 0 0 1 1
> 1 0 1 0 0 0
> 0 0 1 0 1 0
>
> % Bruno

hi Bruno Luong
thanks for the solution and your time. this solution works well with small example. the actuall size of dataset i am working on is big. it has 1049866 rows in array A. and when i applied your solution to that dataset it gives me following error massege:

??? Error using ==> accumarray
Maximum variable size allowed by the program is exceeded.

now what shud i do? plz help
Thanks
 

Subject: Creating Adjacency Matrix from a dataset

From: Bruno Luong

Date: 10 Apr, 2013 09:28:06

Message: 5 of 8

"Tehmina" wrote in message <kk33jl$nl2$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kk0n50$639$1@newscl01ah.mathworks.com>...
> > A=[0 1
> > 0 2
> > 0 5
> > 1 2
> > 1 3
> > 1 4
> > 2 3
> > 2 5
> > 3 1
> > 3 4
> > 3 5
> > 4 0
> > 4 2
> > 5 2
> > 5 4];
> >
> > >> accumarray(A+1,1)
> >
> > ans =
> >
> > 0 1 1 0 0 1
> > 0 0 1 1 1 0
> > 0 0 0 1 0 1
> > 0 1 0 0 1 1
> > 1 0 1 0 0 0
> > 0 0 1 0 1 0
> >
> > % Bruno
>
> hi Bruno Luong
> thanks for the solution and your time. this solution works well with small example. the actuall size of dataset i am working on is big. it has 1049866 rows in array A. and when i applied your solution to that dataset it gives me following error massege:
>
> ??? Error using ==> accumarray
> Maximum variable size allowed by the program is exceeded.
>
> now what shud i do? plz help
> Thanks
>

Create in sparse format:

accumarray(A+1,1,[],[],0,true)

or equivalently use sparse() command

sparse(A(:,1)+1,A(:,2)+1,1)

% Bruno

Subject: Creating Adjacency Matrix from a dataset

From: Tehmina

Date: 16 Apr, 2013 14:39:08

Message: 6 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kk3bb6$e55$1@newscl01ah.mathworks.com>...
> "Tehmina" wrote in message <kk33jl$nl2$1@newscl01ah.mathworks.com>...
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kk0n50$639$1@newscl01ah.mathworks.com>...
> > > A=[0 1
> > > 0 2
> > > 0 5
> > > 1 2
> > > 1 3
> > > 1 4
> > > 2 3
> > > 2 5
> > > 3 1
> > > 3 4
> > > 3 5
> > > 4 0
> > > 4 2
> > > 5 2
> > > 5 4];
> > >
> > > >> accumarray(A+1,1)
> > >
> > > ans =
> > >
> > > 0 1 1 0 0 1
> > > 0 0 1 1 1 0
> > > 0 0 0 1 0 1
> > > 0 1 0 0 1 1
> > > 1 0 1 0 0 0
> > > 0 0 1 0 1 0
> > >
> > > % Bruno
> >
> > hi Bruno Luong
> > thanks for the solution and your time. this solution works well with small example. the actuall size of dataset i am working on is big. it has 1049866 rows in array A. and when i applied your solution to that dataset it gives me following error massege:
> >
> > ??? Error using ==> accumarray
> > Maximum variable size allowed by the program is exceeded.
> >
> > now what shud i do? plz help
> > Thanks
> >
>
> Create in sparse format:
>
> accumarray(A+1,1,[],[],0,true)
>
> or equivalently use sparse() command
>
> sparse(A(:,1)+1,A(:,2)+1,1)
>
> % Bruno

Thanks a lot for your help
i have used ur sparse commad and it works well.
now i have another problem
the following is my code:

%% Sparse power method
n = 425875;
i = data(:,[1]);
j = data(:,[2]);
G=sparse(data(:,1)+1,data(:,2)+1,1);

%% Page Rank
p = 0.85;
delta = (1-p)/n;
c = sum(G,1);
k = find(c~=0);
D = sparse(k,k,1./c(k),n,n);
e = ones(n,1);j
I = speye(n,n);
x = (I - p*G*D)\e;
x = x/sum(x)

over here this command is taken from ur help
G=sparse(data(:,1)+1,data(:,2)+1,1); only A is replaced with data

but the command D = sparse(k,k,1./c(k),n,n); is giving error
the following is the error msg

??? Error using ==> sparse
Index exceeds matrix dimensions.

Error in ==> reading2 at 14
D = sparse(k,k,1./c(k),n,n);

can u plz correct this statement
thanks for ur time....

Subject: Creating Adjacency Matrix from a dataset

From: Pramit Mazumdar

Date: 29 Oct, 2013 07:07:06

Message: 7 of 8

Hi,
I had read all the above posts but still facing problem handling my dataset.
i am having an array X,
X = [ 0, 77; 0, 88; 0, 99; 1, 88; 1, 66; 2, 99 ]

both the functions accumarray() and sparse() are producing 100 columns, as because maximum value in column - 2 is 99.
But, I need the following output:

0 1 1 1
1 0 1 0
0 0 0 1

I need the adjacency matrix with only 4-columns representing all the unique values in column-2.

Please help.

Subject: Creating Adjacency Matrix from a dataset

From: John D'Errico

Date: 29 Oct, 2013 10:36:10

Message: 8 of 8

"Pramit Mazumdar" wrote in message <l4nmqq$mmr$1@newscl01ah.mathworks.com>...
> Hi,
> I had read all the above posts but still facing problem handling my dataset.
> i am having an array X,
> X = [ 0, 77; 0, 88; 0, 99; 1, 88; 1, 66; 2, 99 ]
>
> both the functions accumarray() and sparse() are producing 100 columns, as because maximum value in column - 2 is 99.
> But, I need the following output:
>
> 0 1 1 1
> 1 0 1 0
> 0 0 0 1
>
> I need the adjacency matrix with only 4-columns representing all the unique values in column-2.
>
> Please help.

Use unique to produce indexes. Look at the third
argument returned from unique. What does it tell
you?

Then use either accumarray or sparse as you desire.

John

Tags for this Thread

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.

Contact us