0

For digraph, I want to use non-positive integer as numeric node ID. e.g;

A = [-1  1  3];
B = [ 3  2  0];
plot(addedge(digraph, A , B))

If I run this, I receive this:

Error using digraph/addedge>validateNodeIDs (line 155)

Numeric node IDs must be positive integers.

Community
  • 1
  • 1
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58

1 Answers1

0

Although, digraph does not allow Node IDs to be non-positive which, I think, is not a good feature but I've made it work using the following programming way:-

The matrices for which we want to make digraph are:

A = [-1  1  3];
B = [ 3  2  0];

Now instead of using the approach according to digraph's documentation i.e. plot(addedge(digraph, A , B)), use the following code instead:

ax=plot(addedge(digraph,(-min([A,B])+1)*ones(size(A))+A,(-min([A,B])+1)*ones(size(B))+B)); 
ax.NodeLabel=strsplit(num2str(min([A,B]):max([A,B])),' ');

This code works for every integer NodeID whether negative, zero or positive.

How it works:-

The strategy I used here is to manipulate the node values so that the minimum value of the node becomes 1. And when digraph is made, node values are manipulated again to display the actual ones.

Steps:-

  1. min([A,B]) finds the minimum value of the matrices A and B.
  2. It is multiplied with the -1 and the result is added with +1. i.e. -min([A,B])+1. So, in the above example, as the minimum value of [A,B] is -1, it is multiplied with -1 which gives +1 then 1 is added into it which gives +2.
  3. The result is then multiplied with ones(size(A)) to get a matrix of all same numbers with same size as A. So, in the above example, the result of (-min([A,B])+1)*ones(size(A)) is [2 2 2]

  4. Now the result is added into the original A matrix which makes the minimum value of [A,B] equals to 1.

  5. Same manipulation is done with matrix B.
  6. Now when finally the digraph is made with manipulated node IDs, its IDs are corrected using: ax.NodeLabel=strsplit(num2str(min([A,B]):max([A,B])),' '); i.e. an array of numbers from minimum value of [A,B] to the maximum value of [A,B] is generated using min([A,B]):max([A,B]) which is then converted to string using num2str, the resulting string is then converted into a cell array which is a requirement for setting node labels.

Output:-

Output

Community
  • 1
  • 1
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58