0

I have the following adjacency matrix:

this is my adjacency matrix

The rows represents B1 to B8 and the column presents W1 to W8.

How can I create a graphical representation of it?

m7913d
  • 10,244
  • 7
  • 28
  • 56

1 Answers1

2

You can use the builtin digraph (introduced in R2015b) function to represent an adjacency matrix:

A = round(rand(8)); % create adjacency matrix
plot(digraph(A)); % plot directed graph

Directed graph

In case of a symmetric matrix, you can plot an undirected graph using graph (introduced in R2015b too):

A = rand(8);
A = round((A+A.')/2); % create a symmetric adjacency matrix
plot(graph(A)); % plot undirected graph

Undirected graph

Converting your adjacency matrix to the expected format

MATLAB expects that the rows ans columns have the same meaning in an adjacency matrix, which is not the case in your question. Therefore, we should add dummy rows (for W1 to W8) and columns (for B1 to B8).

A_ = round(rand(8)); % creates your adjacency matrix
A = [zeros(size(A_))' A_; A_' zeros(size(A_))]; % converts to the expected format

% gives the columns a points a name 
names = {'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'W1', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8'};
plot(graph(A, names)); % create the undirected graph as demonstrated earlier

enter image description here

Alternatives

  • Using gplot may be useful for older MATLAB versions

    gplot(A, [zeros(8, 1) (1:8)'; 5*ones(8, 1) (1:8)'])
    set(gca,'XTick',[0 5])
    set(gca,'XTickLabel',{'B', 'W'})
    

From R0126b, the last two lines may be written somewhat nicer as:

xticks([0 5]);
xticklabels({'B', 'W'})`

enter image description here

Community
  • 1
  • 1
m7913d
  • 10,244
  • 7
  • 28
  • 56
  • W and B are different for first row for example you see it B1 is connected to W1 and W4 – Ebraam Emil May 06 '17 at 18:24
  • I have added a paragraph about converting your matrix to the expected format. – m7913d May 06 '17 at 18:41
  • sorry if very bad in matlab but i tried code http://imgur.com/noskzBO and got http://imgur.com/GkTG4kb – Ebraam Emil May 06 '17 at 18:54
  • 1
    @m7913d Nice answer! The [`graph`](https://mathworks.com/help/matlab/ref/graph.html) and [`digraph`](https://mathworks.com/help/matlab/ref/digraph.html) functions were Introduced in MATLAB R2015b. Since it's a fairly recent release, I suggest adding this somewhere in your answer. – codeaviator May 06 '17 at 18:55
  • @EbraamEmil What version of MATLAB are you using? – codeaviator May 06 '17 at 18:58
  • So, you can try the `gplot` approach for that version of MATLAB – m7913d May 06 '17 at 19:37
  • You should not use `graph` with your version of MATLAB, so the error can not be the same or you forgot to remove your old code from your script. – m7913d May 06 '17 at 19:46
  • i tried gplot method with xticks([0 5]) xticklabels({'B', 'W'}) got Undefined function 'xticks' for input arguments of type 'double'. Error in projectbackup (line 189) xticks([0 5]) – Ebraam Emil May 06 '17 at 19:53
  • In that case, your `A` is not correct, you should use the one from _Converting your adjacency matrix to the expected format_. Note that i edited my code – m7913d May 06 '17 at 21:38