% 1. sparse matrices a = sparse(4,6,3,10,10) a = (4,6) 3 spy(a) % a more interesting plot is generated by % the so-called Pascal matrices: p32 = pascal(32); s32 = rem(p32,2); % creates a sparse 0/1 matrix spy(s32) % to reduce this matrix to a sparse matrix : [i,j,s] = find(s32); sp32 = sparse(i,j,s) sp32 = (1,1) 1 (2,1) 1 (3,1) 1 (4,1) 1 (5,1) 1 (6,1) 1 (7,1) 1 (8,1) 1 (9,1) 1 (10,1) 1 (11,1) 1 (12,1) 1 (13,1) 1 (14,1) 1 (15,1) 1 (16,1) 1 (17,1) 1 (18,1) 1 (19,1) 1 (20,1) 1 (21,1) 1 (22,1) 1 (23,1) 1 (24,1) 1 (25,1) 1 (26,1) 1 (27,1) 1 (28,1) 1 (29,1) 1 (30,1) 1 (31,1) 1 (32,1) 1 (1,2) 1 (3,2) 1 (5,2) 1 (7,2) 1 (9,2) 1 (11,2) 1 (13,2) 1 (15,2) 1 (17,2) 1 (19,2) 1 (21,2) 1 (23,2) 1 (25,2) 1 (27,2) 1 (29,2) 1 (31,2) 1 (1,3) 1 (2,3) 1 (5,3) 1 (6,3) 1 (9,3) 1 (10,3) 1 (13,3) 1 (14,3) 1 (17,3) 1 (18,3) 1 (21,3) 1 (22,3) 1 (25,3) 1 (26,3) 1 (29,3) 1 (30,3) 1 (1,4) 1 (5,4) 1 (9,4) 1 (13,4) 1 (17,4) 1 (21,4) 1 (25,4) 1 (29,4) 1 (1,5) 1 (2,5) 1 (3,5) 1 (4,5) 1 (9,5) 1 (10,5) 1 (11,5) 1 (12,5) 1 (17,5) 1 (18,5) 1 (19,5) 1 (20,5) 1 (25,5) 1 (26,5) 1 (27,5) 1 (28,5) 1 (1,6) 1 (3,6) 1 (9,6) 1 (11,6) 1 (17,6) 1 (19,6) 1 (25,6) 1 (27,6) 1 (1,7) 1 (2,7) 1 (9,7) 1 (10,7) 1 (17,7) 1 (18,7) 1 (25,7) 1 (26,7) 1 (1,8) 1 (9,8) 1 (17,8) 1 (25,8) 1 (1,9) 1 (2,9) 1 (3,9) 1 (4,9) 1 (5,9) 1 (6,9) 1 (7,9) 1 (8,9) 1 (17,9) 1 (18,9) 1 (19,9) 1 (20,9) 1 (21,9) 1 (22,9) 1 (23,9) 1 (24,9) 1 (1,10) 1 (3,10) 1 (5,10) 1 (7,10) 1 (17,10) 1 (19,10) 1 (21,10) 1 (23,10) 1 (1,11) 1 (2,11) 1 (5,11) 1 (6,11) 1 (17,11) 1 (18,11) 1 (21,11) 1 (22,11) 1 (1,12) 1 (5,12) 1 (17,12) 1 (21,12) 1 (1,13) 1 (2,13) 1 (3,13) 1 (4,13) 1 (17,13) 1 (18,13) 1 (19,13) 1 (20,13) 1 (1,14) 1 (3,14) 1 (17,14) 1 (19,14) 1 (1,15) 1 (2,15) 1 (17,15) 1 (18,15) 1 (1,16) 1 (17,16) 1 (1,17) 1 (2,17) 1 (3,17) 1 (4,17) 1 (5,17) 1 (6,17) 1 (7,17) 1 (8,17) 1 (9,17) 1 (10,17) 1 (11,17) 1 (12,17) 1 (13,17) 1 (14,17) 1 (15,17) 1 (16,17) 1 (1,18) 1 (3,18) 1 (5,18) 1 (7,18) 1 (9,18) 1 (11,18) 1 (13,18) 1 (15,18) 1 (1,19) 1 (2,19) 1 (5,19) 1 (6,19) 1 (9,19) 1 (10,19) 1 (13,19) 1 (14,19) 1 (1,20) 1 (5,20) 1 (9,20) 1 (13,20) 1 (1,21) 1 (2,21) 1 (3,21) 1 (4,21) 1 (9,21) 1 (10,21) 1 (11,21) 1 (12,21) 1 (1,22) 1 (3,22) 1 (9,22) 1 (11,22) 1 (1,23) 1 (2,23) 1 (9,23) 1 (10,23) 1 (1,24) 1 (9,24) 1 (1,25) 1 (2,25) 1 (3,25) 1 (4,25) 1 (5,25) 1 (6,25) 1 (7,25) 1 (8,25) 1 (1,26) 1 (3,26) 1 (5,26) 1 (7,26) 1 (1,27) 1 (2,27) 1 (5,27) 1 (6,27) 1 (1,28) 1 (5,28) 1 (1,29) 1 (2,29) 1 (3,29) 1 (4,29) 1 (1,30) 1 (3,30) 1 (1,31) 1 (2,31) 1 (1,32) 1 i = [1:10 2:10 1:9]; % row indices with nonzeros j = [1:10 1:9 2:10]; % corresponding columns s = [2*ones(1,10) ones(1,9) ones(1,9)]; b = sparse(i,j,s); spy(b) eig(b) ans = 0.0810 0.3175 0.6903 1.1692 1.7154 2.2846 2.8308 3.3097 3.6825 3.9190 % 2. communication networks % a graph is represented by its adjency matrix: % A(i,j) = 1 or 0 depending whether there an % edge between i and j dt = 2*pi/10; t = dt:dt:2*pi; x = cos(t); y = sin(t); % (x,y) define the nodes on the unit circle size(x) ans = 1 10 plot(x,y,'r+'); axis([-1.5 1.5 -1.5 1.5]); % assuming that every node is connected to % every other node A = ones(10); size(A) ans = 10 10 gplot(A,[x' y']) A = spalloc(10,10,9); for j = 1:9 if mod(j,2) == 1 A(5,j) = 1; else A(10,j) = 1; end; end; A(5,5) = 0; A(5,10) = 1; gplot(A,[x' y']) % note that A is now the adjacency matrix % of a directed graph, because it is not symmetric spy(A) B = A + A' B = (5,1) 1 (10,2) 1 (5,3) 1 (10,4) 1 (1,5) 1 (3,5) 1 (7,5) 1 (9,5) 1 (10,5) 1 (10,6) 1 (5,7) 1 (10,8) 1 (5,9) 1 (2,10) 1 (4,10) 1 (5,10) 1 (6,10) 1 (8,10) 1 spy(B) gplot(B,[x' y']); C = B^2; spy(C) D = B^3; spy(D) % C tells which nodes are connected to each other % using two links, as C = B^2; % D indicates which nodes are connected via 3 edges E = A + C + D; spy(E) spy(B+C) % we see that we need 3 edges to make the graph % completely connected... % 3. Four dimensional matrices % consider the temperature distribution in a room r = -2:0.1:+2; % defines a range [x,y,z] = ndgrid(r,r,r); % sampling grid % we defined a grid using the same range % for x, y, and z, to see how many samples we used: size(x) ans = 41 41 41 t = y.*exp(-x.^2 - y.^2 - z.^2); slice(y,x,z,t,[],[-1.2 .8],-0.2); hold on xlabel('x'); ylabel('y'); zlabel('z'); hold off cplxroot(5) exit