-1

I am learning about recursive functions. I want to be able to output a row vector 'n' of pascals triangle. k is the element of that row. For example, n = 3, row = 1 2 1.

So far I have tried this, yet unsuccessful. The error I get is that I need to define PT once n > 2. I've looked at other questions on stack overflow however none of them show recursive functions in MATLAB. I tried using a for-loop when n > 2 but that didn't seem to work either.

function row = PT(n,k)

    if n==1
        k = 1;
        PT(n,k) = 1;
        row = [1];
    elseif n==2
        if k == 1
            PT(n,k) = 1;
        else k == 2
            PT(n,k) = 1;
        end
        row = [1 1];
        
    else n > 2;
     
        PT(1,1) = [1];
        PT(2,1) = [1];
        PT(2,2) = [1];
        row = [PT(2,1), PT(2,2)];
        row = [PT(n-1,k-1)+ PT(n-1,k)];
           
end
end
Amber
  • 43
  • 4
  • 1
    Why do you pass `k` to the function if you want to return the whole row? Statements like `PT(n,k) = 1;` make no sense. You can't assign a value to the return value of a function. What do you expect to happen here? – Thomas Sablik Feb 16 '21 at 13:02
  • `PT(1,1) = [1]` ? But `PT` is the name of the function. You can not assign values to the function call – Ander Biguri Feb 16 '21 at 13:07
  • Please bare with me as I'm just starting, I was trying to create a row which had each value of PT(n,k) where 1 < k < n and if k = 1 or k = n, then that represents each end of the row being defined as 1. – Amber Feb 16 '21 at 13:23

2 Answers2

0

Pascal's triangle is not a classical recursive problem and can easily be solved in a loop:

function row = PT(n)
    row = 1;
    for z = 2:n
        row = [row(1), movsum(row, 2, 'Endpoints', 'discard'), row(end)];
    end
end

But of course, you can convert every loop to a recursion:

function row = PT(n)
    row = recursiveHelper(1, n);
end

function row = recursiveHelper(row, n)
    if n == 1
        return;
    end
    row = recursiveHelper([row(1), movsum(row, 2, 'Endpoints', 'discard'), row(end)], n - 1);
end

A recursive function calls itself. In this case recursiveHelper calculates the next row from the current row, decrements n and calls itself until n reaches 1.

Both functions produce the same output:

>> PT(1)
ans = 1
>> PT(2)
ans = 1 1
>> PT(3)
ans = 1 2 1
>> PT(4)
ans = 1 3 3 1
>> PT(5)
ans = 1 4 6 4 1
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • This may seem obvious, but how do I implement 2 functions into one script? Or is the PT function and recursive helper made in 2 scripts and then called for? – Amber Feb 16 '21 at 13:38
  • @DevinaArts You can add as many functions as you want into one script but only the first function is callable from outside and only if there is no code before. All other functions are local to the script. – Thomas Sablik Feb 16 '21 at 13:41
-1

After spending more time on this, I was correct to add a for-loop and wrong with the naming of my function.

Below is the refined code:

function [out] = mypascalrow(n)
% The first row is a 1, and the second is 1 1. 
PT(1,1) = 1;
PT(2,1) = [1 1];
PT(2,2) = [1 1];
% first if statement is establishing the first 2 rows
if n == 1
    out = [1];
elseif n == 2
    out = [1 1];
end
 
for i = 3:n
    PT(i,1) = 1; % starting with a 1
    
    for k = 2:i-1
        PT(i,k) = PT(i-1, k-1) + PT(i-1, k);
    end
    
    PT(i,i) = 1; % ending with a 1
end
out = PT(n,:);
end

Tested results:

>> [out] = mypascalrow(7)
out =
     1     6    15    20    15     6     1
Amber
  • 43
  • 4