MATLAB exercise program (matrix generation of undirected network diagram)

Source: Internet
Author: User

There is a network in the dormitory, and the matrix needs to be converted into a network map to make it look a little bit. So I wrote such a function here.

The function is to change the adjacent matrix or associated matrix into a network diagram, but I can only convert it to an undirected graph here. I still need to study the arrow of the directed graph.The annotation function can be called.

 % Function name netplot % use method input help netplot % No Return Value
% Function can only process undirected graph % Author: tiandsp % last modification: 2012.12 . 26 Function Netplot (A, flag) % call Method Input netplot (A, flag), no return value % A is an adjacent matrix or association matrix % flag = 1 % Flag = 2 When processing the association matrix % function, only undirected graphs can be processed. If Flag =1 % Adjacent matrix indicates undirected graph nd_netplot (a); return; End If Flag = 2 % Join matrix: undirected graph [m n] = Size (a); % W = Zeros (M, M ); For I = 1 : N = Find (A (:, I )~ = 0 ); W (( 1 ), ( 2 ) = 1 ; W (( 2 ), ( 1 ) = 1 ; End Nd_netplot (w); return; End Function Nd_netplot (a) [N] = Size (a); W = Floor (SQRT (n); H = Floor (N/ W); x = []; Y = []; For I = 1 : H % gives the range of random points to be generated, so that the display distribution is wider. For J = 1 : W x = [X 10 * Rand ( 1 ) + (J- 1 )* 10 ]; Y = [Y 10 * Rand ( 1 ) + (I- 1 )* 10 ]; End End Ed = N-H * W; For I = 1 : Ed x = [X 10 * Rand (1 ) + (I- 1 )* 10 ]; Y = [Y 10 * Rand ( 1 ) + H * 10 ]; End Plot (x, y, ' R * ' ); Title ( ' Network Topology ' ); For I = 1 : N For J = I: N If A (I, j )~ = 0 C = Num2str (a (I, j); % convert the weight in a to plain text (X (I) + X (j ))/ 2 , (Y (I) + Y (j ))/2 , C, ' Fontsize ' , 10 ); % Display Edge Weight line ([X (I) x (j)], [Y (I) y (j)]); % line End Text (X (I), y (I), num2str (I ), ' Fontsize ' , 14 , ' Color ' , ' R ' ); % Display vertex serial number hold on; End End End End

I will not say much about the call methods.

Below is a graph with 10 nodes (I still like to upload images for image creation ):

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.