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) Nonvectorized 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(i1) + p(i);
end
p= q;
% Update p
end
% p=q is actually vectorized.
Truly nonvectorized 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, c1) + 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.
 Spring '07
 FAN/VANLOAN

Click to edit the document details