Attachment 'Aufgabe4.m'

Download

   1 function A = Aufgabe4(X, k)
   2 %Aufgabe4 - compute and plot k-nn graph, histogram of node degrees and edge lengths.
   3 %
   4 %usage
   5 %  A = Aufgabe4(X, k)
   6 %
   7 %input
   8 %  X : (2,n)-matrix of column vectors.
   9 %  k : Number of nearest neighbours. 
  10 %
  11 %output
  12 %  A : (n,n)-adjacency matrix, where A_ij == 1 iff vertices i and j are connected.
  13 %
  14 %description
  15 %  Aufgabe4 computes the k-nearest-neighbour graph on the columns in X and plots
  16 %  the histogram of node degrees and edge lengths.
  17 %
  18 %author
  19 %  buenau@cs.tu-berlin.de
  20 
  21 [d, n] = size(X);
  22 
  23 % Compute squared distance matrix.
  24 xx = sum( X .^ 2, 1);
  25 D = repmat(xx, [n 1]) -2*X'*X + repmat(xx', [1 n]);
  26 
  27 % Build adjacency matrix. 
  28 [foo, ii] = sort(D);
  29 A = zeros(n, n);
  30 for i=1:n
  31   A(i,ii(2:k+1, i)) = 1;
  32 end
  33 A = double(A | A');
  34 
  35 % Plot graph.
  36 subplot(3, 1, 1);
  37 plot(X(1,:), X(2,:), 'bo');
  38 hold on;
  39 At = triu(A);
  40 [ii, jj] = find(At);
  41 
  42 for i=1:length(ii)
  43   h = line( [ X(1,ii(i)) X(1,jj(i)) ], [ X(2,ii(i)) X(2,jj(i)) ] );
  44   set(h, 'LineStyle', '--');
  45 end
  46 title('Graph');
  47 
  48 % Histogram of node degrees.
  49 subplot(3, 1, 2);
  50 hist(sum(A, 1));
  51 title('Histogram of degrees');
  52 
  53 % Histogram of edge lengths.
  54 subplot(3, 1, 3);
  55 hist(D(find(At)));
  56 title('Histogram of edge lengths');

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-03-12 14:03:20, 1.5 KB) [[attachment:Aufgabe1.m]]
  • [get | view] (2009-03-12 14:03:20, 0.8 KB) [[attachment:Aufgabe2.m]]
  • [get | view] (2009-03-12 14:03:20, 1.0 KB) [[attachment:Aufgabe3.m]]
  • [get | view] (2009-03-12 14:03:20, 1.2 KB) [[attachment:Aufgabe4.m]]
  • [get | view] (2009-03-12 14:03:20, 1.5 KB) [[attachment:Aufgabe5.m]]
  • [get | view] (2009-03-12 14:03:20, 88.7 KB) [[attachment:ML_Praktikum_U01.pdf]]
  • [get | view] (2009-03-12 14:03:20, 89.4 KB) [[attachment:ML_Praktikum_U02.pdf]]
  • [get | view] (2009-03-12 14:03:20, 78.5 KB) [[attachment:ML_Praktikum_U03.pdf]]
  • [get | view] (2009-03-12 14:03:20, 96.2 KB) [[attachment:ML_Praktikum_U04.pdf]]
  • [get | view] (2009-03-12 14:03:20, 74.4 KB) [[attachment:ML_Praktikum_U05.pdf]]
  • [get | view] (2009-03-12 14:03:20, 86.7 KB) [[attachment:ML_Praktikum_U06.pdf]]
  • [get | view] (2009-03-12 14:03:20, 80.0 KB) [[attachment:Matlab_diary.txt]]
  • [get | view] (2009-03-12 14:03:20, 6.3 KB) [[attachment:U03_2gaussians.dat]]
  • [get | view] (2009-03-12 14:03:20, 15.6 KB) [[attachment:U03_5gaussians.dat]]
  • [get | view] (2009-03-12 14:03:20, 1020.4 KB) [[attachment:U04_datasets.zip]]
  • [get | view] (2009-03-12 14:03:20, 6.4 KB) [[attachment:U05_datasets.zip]]
  • [get | view] (2009-03-12 14:03:20, 60.1 KB) [[attachment:fishbowl.mat]]
  • [get | view] (2009-03-12 14:03:20, 22.7 KB) [[attachment:flatroll.mat]]
  • [get | view] (2009-03-12 14:03:20, 22.7 KB) [[attachment:flattroll.mat]]
  • [get | view] (2009-03-12 14:03:20, 343.4 KB) [[attachment:guide.pdf]]
  • [get | view] (2009-03-12 14:03:20, 32.8 KB) [[attachment:long_words.mat]]
  • [get | view] (2009-03-12 14:03:20, 7.6 KB) [[attachment:ratbert-debate.mat]]
  • [get | view] (2009-03-12 14:03:20, 45.3 KB) [[attachment:swissroll.mat]]
  • [get | view] (2009-03-12 14:03:20, 69.4 KB) [[attachment:tu_logo.mat]]
  • [get | view] (2009-03-12 14:03:20, 4053.5 KB) [[attachment:usps.mat]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.