Question: In pyschools, I am stuck in the power of 2 recursive function

```

    >>> createStars(0)      # 2 to power of 0 = 1
    '*'
    >>> createStars(1)      # 2 to power of 1 = 2
    '**'
    >>> createStars(2)      # 2 to power of 2 = 4
    '****'
    >>> createStars(3)      # 2 to power of 3 = 8
    '********'

```

What I am trying to do is as following:

```

def createStars(x):
    if x == 0:
        return '*'
    else:
        return '*' * x + createStars(x-1) 

```

However, this seems to be a summation of 'x' not power of 2.
Meaning, this will break when the x is higher than 2

I know how to do the power of 2 recursively but no idea where to change to make createStars() work.

```

def power(x, n):
    if n == 0:
        return 1
    else:
        return x * power(x, n-1)

```

PS. I know it is easy to use non-recursive method to solve it. 
 But would like to seek advice how to do it in recursive way. 

Thanks.

Answer: 
Here are two options, either look for where the element of `B` matches an element of `A`, which could fail if `A` has repeats, or by using the `C` matrix, look for the n'th one.

```
A = [12 52 12 62 42];
C = logical([1 1 0 1 0])
B = A(C)

[~,idx]=find(A==B(3)) %// finds where the third element of B was in A
idx=find(cumsum(C)==3,1) %// finds where the third 1 in C is

```
[`find`](http://es.mathworks.com/help/matlab/ref/find.html) gives the answer. Let `indB` denote the index in `B`. Then the index in `A`, denoted `indA`, is 

```
indA = find(C); %// find nonzeros in C
indA = indA(indB); %// take the indB-th nonzero in C

```