12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

first row <strong>of</strong> a by taking the columnar sum <strong>of</strong> the triangular matrix s:a(1,:) = sum(s)⎛⎞1 1 1 1 1 ···0 2 2 2 20 0 3 3 3= sum0 0 0 4 4⎜⎝0 0 0 0 5 ⎟⎠....We can generate the second row <strong>of</strong> a by taking the same columnar sumbut leaving out the first row <strong>of</strong> s:a(2,:) = sum(s(2:end,:))⎛⎞0 2 2 2 2 ···0 0 3 3 3= sum0 0 0 4 4⎜⎝0 0 0 0 5 ⎟⎠....In general, then, we can generate the ith row <strong>of</strong> a by taking the columnarsum <strong>of</strong> s leaving out its first i − 1 rows: a(i,:) = sum(s(i:end,:)).Our final code will consist <strong>of</strong> putting this statement inside a for loop(this will be a good use <strong>of</strong> a for loop—see the first paragraph in thissection). Before we do that, though, we still need to generate the utilitymatrix s; here we can use matrix multiplication. The matrix we wantcan be obtained by taking the upper triangular part <strong>of</strong> the product <strong>of</strong> acolumn vector <strong>and</strong> a row vector:⎛⎞ ⎡⎛⎞⎤1 1 1 1 1 ···10 2 2 2 220 0 3 3 330 0 0 4 4= triu4· ( 1 1 1 1 1··· )⎜⎝0 0 0 0 5 ⎟ ⎢⎜⎠ ⎣⎝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-1a(i,:) = sum(s(i:end,:));enda(N,:) = s(N,:);..c○ 2000 by CRC Press LLC

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!