7.1 From matrix to insight

  • Many data types can be represented by matrices: tables, graphs, images
  • For a graph with adjacency matrix A, Ak represents the number of ways to walk from i to j in k steps

Exercise 5.1.5

datafile = download("https://tobydriscoll.net/fnc-julia/_static/resources/actors.jld2")
@load datafile A

What is the maximum number of actors appearing in any one movie?
Maximum row sum.
findmax(itr) -> (x, index)

A[1,:];
sum(A[1,:]);

s = [sum(A[i,:]) for i = 1:size(A,1)];
findmax(s)

How many actors appeared in exactly three movies?
Find how many column sums equal 3

s2 = [sum(A[:,i]) for i = 1:size(A,2)];

sum(@. s2 == 3)

Define C = ATA. How many nonzero entries does C have? What is the interpretation of C?
C is an actor by actor matrix where the nonzero entries correspond to how many movies two actors have been in together or on the diagonal it is how many movies an actor has been in.

C = A'*A;
C[1:5, 1:5]

sum(@. C > 0)
reduce(*, size(C))