Page 190 - Basics of MATLAB and Beyond
P. 190

first row of a by taking the columnar sum of the triangular matrix s:
                                           a(1,:)  = sum(s)
                                                             11111          ···
                                                                              
                                                             02222
                                                                              
                                                                              
                                                             00333
                                                                              
                                                                              
                                                             00044              .
                                                   = sum 
                                                                              
                                                             00005
                                                                              
                                                             .              .
                                                                              
                                                             .              .
                                                             .               .
                               We can generate the second row of a by taking the same columnar sum
                               but leaving out the first row of s:
                                           a(2,:)  = sum(s(2:end,:))
                                                                              
                                                             02222          ···
                                                             00333
                                                                              
                                                                              
                                                             00044
                                                                              
                                                   = sum                       .
                                                             00005
                                                                              
                                                             .              .
                                                                              
                                                             .              .
                                                             .               .
                               In general, then, we can generate the ith row of a by taking the columnar
                               sum of s leaving out its first i − 1 rows: a(i,:) = sum(s(i:end,:)).
                               Our final code will consist of putting this statement inside a for loop
                               (this will be a good use of a for loop—see the first paragraph in this
                               section). Before we do that, though, we still need to generate the utility
                               matrix s; here we can use matrix multiplication. The matrix we want
                               can be obtained by taking the upper triangular part of the product of a
                               column vector and a row vector:
                                                                                     
                                   11111          ···             1
                                   02222                          2
                                                                                     
                                                                                     
                                   00333                          3
                                                                                     

                                                                                     
                                   00044                          4   ·
                                                      = triu         11111 ··· 
                                                                                     
                                   00005                          5
                                                                                     
                                   . . .          . . .           . . .
                                                                                     
                               So here we have the final code to generate the a matrix (for N = 200):
                               N = 200;
                               s = triu((1:N)’*ones(1,N));
                               a = zeros(N,N);
                               for i = 1:N-1
                                 a(i,:) = sum(s(i:end,:));
                               end
                               a(N,:) = s(N,:);
                               c   2000 by CRC Press LLC
   185   186   187   188   189   190   191   192   193   194   195