How to generate a sparse matrix for a given array?

Dear All,
I want to generate a sparse matrix B for a given array A. A contains two columns of indecis. For example,
A = [1 3;
1 4;
2 3;
2 5;
3 5;
4 5]
The sparse matrix B should be as follows:
B = [ 2 0 -1 -1 0;
0 2 -1 0 -1;
-1 -1 2 0 0;
-1 0 0 2 -1;
0 -1 -1 -1 3]
The characteristics of B:
  1. Sum of each row is zero.
  2. If we consider matrix A gives the information of edges of a graph, B(i,i) = sum of number of edges, B(i,j) = -1 if there is an edge between i and j.
Thanks a lot.
Benson

 Akzeptierte Antwort

Matt J
Matt J am 25 Mai 2021
Bearbeitet: Matt J am 25 Mai 2021
B=laplacian( graph(A(:,1),A(:,2)) );

5 Kommentare

Hi, Matt,
I tried your code, but I got the following error:
"Graph has multiple edges."
Thanks a lot.
Benson
Works fine for me:
A = [1 3;
1 4;
2 3;
2 5;
3 5;
4 5];
B=laplacian( graph(A(:,1),A(:,2)) );;
full(B)
ans = 5×5
2 0 -1 -1 0 0 2 -1 0 -1 -1 -1 3 0 -1 -1 0 0 2 -1 0 -1 -1 -1 3
Matt J
Matt J am 25 Mai 2021
Bearbeitet: Matt J am 25 Mai 2021
You probably need to do,
A=unique(sort(A,2),'rows')
yes, it works well. Thanks a lot.
Benson
Matt J
Matt J am 25 Mai 2021
You're welcome, but please Accept-click the answer that you deem best resolves your question.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Here is one way:
A = [1 3;
1 4;
2 3;
2 5;
3 5;
4 5];
d = max(A(:));
B = sparse(A(:,1),A(:,2),-1,d,d) + sparse(A(:,2),A(:,1),-1,d,d);
for ii = 1:d
B(ii,ii) = -sum(B(ii,:));
end
disp(B)
(1,1) 2 (3,1) -1 (4,1) -1 (2,2) 2 (3,2) -1 (5,2) -1 (1,3) -1 (2,3) -1 (3,3) 3 (5,3) -1 (1,4) -1 (4,4) 2 (5,4) -1 (2,5) -1 (3,5) -1 (4,5) -1 (5,5) 3

7 Kommentare

Hi, the Cyclist,
Thanks for your answer. It looks pretty good. But I am wondering if it is possible to avoid using the for iterations, because matrix A has 5000 rows.
Thanks a lot again.
Benson
A = [1 3;
1 4;
2 3;
2 5;
3 5;
4 5];
d = max(A(:));
B = sparse(A(:,1),A(:,2),-1,d,d) + sparse(A(:,2),A(:,1),-1,d,d);
% for ii = 1:d
% B(ii,ii) = -sum(B(ii,:));
% end
B = B - diag(sum(B,2));
disp(B)
(1,1) 2 (3,1) -1 (4,1) -1 (2,2) 2 (3,2) -1 (5,2) -1 (1,3) -1 (2,3) -1 (3,3) 3 (5,3) -1 (1,4) -1 (4,4) 2 (5,4) -1 (2,5) -1 (3,5) -1 (4,5) -1 (5,5) 3
Hi, Cyclist,
Thanks a lot for your prompt reply.
I tried your code on my case with 5000 rows in A. But I found there are some different results between your code and my original code which uses for iterations.
For example, row 3 in B, yours is
(1,5) 8
(1,7) -1
(1,28) -3
(1,33) -1
(1,34) -1
(1,397) -1
(1,434) -1
My result is
(1,5) 6
(1,7) -1
(1,28) -1
(1,33) -1
(1,34) -1
(1,397) -1
(1,434) -1
The locations of non-zero elemnts are correct, but the values are different. The differency may be caused by the double edges between two nodes. There are 2 edges between 5 and 28.
Thanks
Benson
(1,28) -3
"There are 2 edges between 5 and 28."
Make it three.
Hi, Bruno,
Instead of making it 3, it should be -1, as my code did. It means I would like to ignore the mulitple edges between two nodes.
Thanks.
Benson
I assume that doing what @Matt J suggested in his answer:
A=unique(sort(A,2),'rows')
is what you would need to do here as well.
If you posted a small example that exhibits the problem, it would help. But I'm guessing you have your answer.
Hi, the Cyclist,
Thanks a lot for your great help. You have a good day!
Benson

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by