Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.68 KB | None | 0 0
  1. % https://uk.mathworks.com/help/matlab/ref/digraph.html
  2.  
  3. row = 3;
  4. col = 5;
  5.  
  6. filename = strcat( int2str(row), 'x', int2str(col), '.csv' ); % examle: 3x5.csv
  7.  
  8. if exist(filename, 'file')  == 0 % Jeśli pliku nie ma, stwórz przykładową macierz
  9.     X = magic(col);               % macierz kwadratowa
  10.     M = reshape(X,3,[]) ;    % trzy wiersze
  11.     csvwrite(filename,M);
  12.  end
  13.  
  14. %  M = [
  15. %         1,3,1;  
  16. %         2,1,3;  
  17. %         2,8,3   % waga
  18. %         ];
  19.  
  20. Sx=0;
  21. M = csvread(filename)
  22. for i=1:size(M,2)
  23.     for k=1:size(M,1)
  24.     Sx = M(i,k)*i*k+Sx
  25.     end
  26. end
  27.  
  28. s = M(1,:);  % Pierwszy wiersz macierzy M
  29. t = M(2,:);
  30. A = { M(1,:) , M(2,:), M(3,:) };
  31. weights  =  M(3,:);
  32.  
  33. tmp = size(M); % dimensions
  34. names = get_names( tmp(1)*tmp(2) ) ;   % Ilość liter alfabetu do wygeneroania. Dla każdego elementu macierzy
  35.  
  36. G = digraph(s,t,weights,names);
  37.  
  38. plot(G,'Layout','force','EdgeLabel',G.Edges.Weight);
  39.  
  40.      % Construct the same digraph as in the previous example using two
  41.         % tables to specify edge and node properties.
  42.         s = [1 1 1 2 2 3 3 4 5 5 6 7]';
  43.         t = [2 4 8 3 7 4 6 5 6 8 7 8]';
  44.         weights = [10 10 1 10 1 10 1 1 12 12 12 12]';
  45.         names = {'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H'}';
  46.         EdgeTable = table([s t],weights,'VariableNames',{'EndNodes' 'Weight'})
  47.         NodeTable = table(names,'VariableNames',{'Name'})
  48.         G = digraph(EdgeTable,NodeTable)
  49.  
  50. plot(G,'Layout','force','EdgeLabel',G.Edges.Weight);
  51.  
  52. function  letters_matrix = get_names(matrix_length)
  53.  numbers=1:matrix_length;                       %musi być mnie niż 26 bo tyle mamy liter alfabetu
  54.  letters=char(numbers+64);
  55.  letters_matrix = num2cell(letters);
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement