finalReviewSol

# finalReviewSol - Solutions Final Review Questions%Question...

This preview shows pages 1–2. Sign up to view the full content.

Solutions Final Review Questions ------------------------------------ %Question 1 function p = pascalVector(lev) % p is the vector corresponding to level lev of Pascal's triangle % % (Mostly) Non-vectorized version p= [1]; % values in level 0 of Pascal's triangle for currentLevel= 1:lev % Temporary vec (q) for current level is one cell longer than prev level q= ones(1,currentLevel+1); % Use the general formula for INTERNAL entries for i= 2:length(p) q(i)= p(i-1) + p(i); end p= q; % Update p end % p=q is actually vectorized. Truly non-vectorized code is: % for i= length(q):-1:1 % p(i)= q(i); % end ---------------------------------- function p = pascalVector(lev) % p is the vector corresponding to level lev of Pascal's triangle % % First build a Pascal's MATRIX, then extract the row m= ones(lev+1, lev+1); for currentLevel= 1:lev for c= 2:currentLevel % "interior" entries only m(currentLevel+1, c) = m(currentLevel, c-1) + m(currentLevel, c); end end p= m(lev+1, :); % p is just the last row of pascal matrix

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This homework help was uploaded on 02/23/2008 for the course CS 100 taught by Professor Fan/vanloan during the Spring '07 term at Cornell.

### Page1 / 3

finalReviewSol - Solutions Final Review Questions%Question...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online