Skip to content

Instantly share code, notes, and snippets.

@schluppeck
Last active October 31, 2019 13:12
Show Gist options
  • Select an option

  • Save schluppeck/c983fec8b683b11c12f52f3c94397623 to your computer and use it in GitHub Desktop.

Select an option

Save schluppeck/c983fec8b683b11c12f52f3c94397623 to your computer and use it in GitHub Desktop.
graphs in matlab

Graphs in Matlab

example of using graph (in mathematical sense) functions. H/t to question from lpycmm during last matlab lab class

ds 2019-10-31, halloween. denis schluppeck

tl;dr

Matlab is awesome and of course there is a way to do it - even though I didn't know too much about all the available functions until I started looking a bit more 😉

solutions

not matlab

Actually, you could look at other options. If you are trying to just visualise graph related data (and not do too many computations), an existing package might be all you need. Things to look for:

Matlab

Have a look at the the example_graphs.m file in this gist to get started. Things to think about are:

  • where does the adjacency matrix come from (data? calculations)
  • what to do for directed versus non-directed graphs.

A minimal example would be:

% mock up a adjacency matrix
% 25 vertices, people, things connected
A = rand(25) < 0.4;
B = triu(A) + tril(A',-1);

% turn into a graph object (vertices and edges)
G = graph(B)

plot(G)
title('a graph!')

% as always - dig through documentation...
doc graph
%% example of using graph (in mathematical sense) functions
%
% ds 2019-10-31, halloween. denis schluppeck
%% other options ;]
%
% If you are planning to do graph related visualisations you may actually
% find very mature solutions elsewhere. There are some very nice
% visualisation packages in rstats and python that will do a very nice job,
% and the overhead of learning a bit of syntax in those languages might
% well be worth your while.
%
% having said that, Matlab is obviously amazing and has some real, useable
% tools in the main distribution, since R2015 at least, looking at the
% documentation.
%
% - look at R, rstats (igraph, igraphdata)
% - python libraries
% - unix DOT, https://en.wikipedia.org/wiki/DOT_(graph_description_language)
% - graphviz (AT&T) https://www.graphviz.org/
%% a simple example
%
% a graph showing the relationship between Vertices using Edges
% doc graph for more details.
%% step 1 - construct a random(ish) ajdacency matrix
A = rand(25) < 0.4; % 25 vertices, people, things connected
% ajdacency matrix needs to be symmetric and full of 0s and 1s
% so take upper triangular part of matrix A
% and add the lower triangular part of the "flipped" version of A... i.e.
% A' -- but making sure to take one less... to not double count the
% diagonal
B = triu(A) + tril(A',-1);
%% step 2 = convince yourself that this is actually a symmetric thing.
figure
subplot(1,2,1)
imagesc(B)
colormap(gray())
axis('image')
hold on
line(get(gca, 'Xlim'), get(gca, 'Ylim'), 'linewidth',2, 'color', 'r');
%% step 3 - simplest version to make graph
% turn into a graph object (vertices and edges)
G = graph(B)
subplot(1,2,2)
p = plot(G, 'markersize', 15, 'nodecolor', 'r', 'linewidth',2, 'edgecolor', 'k', 'edgealpha', 0.7);
axis('square')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment