Skip to content

Instantly share code, notes, and snippets.

@moink
Created October 25, 2011 21:20
Show Gist options
  • Save moink/1314324 to your computer and use it in GitHub Desktop.
Save moink/1314324 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Oct 25, 2011.
    33 changes: 33 additions & 0 deletions transclose.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    function A=transclose(A)
    %returns the transitive closure matrix of the input matrix
    %input: A(i,j) is 1 if the binary relation if (element i) R (element j) using the %notation in the introduction
    %of http://en.wikipedia.org/wiki/Transitive_closure
    %
    %Usage example: Implements the first example problem on https://www.4clojure.com/problem/84
    %
    %divideskey=[2 3 4 8 9 27];
    %%I don't actually use that variable, but it keeps track of what my matrix means
    %
    %divides=zeros(6)
    %
    %divides(4,3)=1 %Add [8,4] to the relation
    %divides(5,2)=1 %Add [9 3] to the relation
    %divides(3,1)=1 %Add [4 2] to the relation
    %divides(6,5)=1 %Add [27 9] to the relation
    %
    %transdiv=transclose(divides);
    %%this returns the matrix
    % transdiv=[
    % 0 0 0 0 0 0
    % 0 0 0 0 0 0
    % 1 0 0 0 0 0
    % 1 0 1 0 0 0
    % 0 1 0 0 0 0
    % 0 1 0 0 1 0];

    for i = 1:size(A,1)
    for j = 1:size(A,2)
    t=(A(i,j)==1 & A(j,:)==1);
    A(i,t)=1;
    end
    end