% 1 sparse matrices % a matrix is sparse if it has few nonzero entries a = sparse(4,6,3,10,10) a = (4,6) 3 spy(a) % with spy we can see patterns p32 = pascal(32); s32 = rem(p32,2); spy(s32) % an application of sparse matrices % turns up in structured linear systems % We defined a tridiagonal matrix % with 2 on the diagonal and ones % above and below the diagonal. rows = [1:10 2:10 1:9]; % rows contain the rows for the 28 % nonzero elements cols = [1:10 1:9 2:10]; % for the column indices we have to % make sure they match the row indices data = [2*ones(1,10) ones(1,9) ones(1,9)]; % the data consists of 10 two's on the diagonal, % nine ones below the diagonal and nine ones % above the diagonal d3 = sparse(rows,cols,data); spy(d3) % 2. model networks % The main tool to model a graph is the adjacency matrix % These matrices are also sparse. % let us draw a graph dt = 2*pi/10; t = dt:dt:2*pi; x = cos(t); y = sin(t); % we just defined 10 vertices, % placed on a circle plot(x,y,'r+') axis([-1.5 1.5 -1.5 1.5]) hold on for i =1:4 text(x(i),y(i)+0.1,int2str(i)) end; text(x(5)-0.1,y(5),int2str(5)) text(x(10)+0.1,y(10),int2str(10)) for i = 6:9 text(x(i),y(i)-0.1,int2str(i)) end; A = ones(10); gplot(A,[x' y']) % gplot takes on input two arguments: % (1) the adjacency matrix A % (2) the coordinates of the vertices % the prime next to x and y: x' and y' % transpose the rows into columns. % 3. multidimensional matrices % A temperature distribution gives rise to a % 4 dimensional matrix. r = -2:0.1:2; [x,y,z] = ndgrid(r,r,r); t = y .* exp(-x.^2 - y.^2 - z.^2); % we just sampled a 3 dimensional temperature function slice(y,x,z,t,[],[-1.2 .8],-.2) figure slice(y,x,z,t,[],[-1.2 .8],-.2) diary off