finalReviewSol - Solutions Final Review Questions...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

finalReviewSol - Solutions Final Review Questions...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online