Stuck in implementing a method for mapping elements to a range : Let there be an array `b = [1,3,2,6,1]` conatining `N = 5` integer valued elements with probability of occurence of each unique integer as `0.4,	0.2,	0.2,	0.2` respectively. The array `b` can take any integers from the unique symbol set `0,1,2,3,4,5,6,7`. Let `n = 8` elements in the symbol set. In essence, the probability for the above data `b` is 
`p= [ 0.4	0.2	0	0.2	0	0	0.2	0]`


An interval `[0,1]` is split into 8 regions. Let, the interval for the data `b` assumed to be known as
   ` Interval_b = [0,	0.4,	0.6,	0.8,	1];`
   
In general, for `n = 8` unique symbols, there are  `n = 8` intervals such as `I_1, I_2, I_3, I_4, I_5, I_6, I_6,I_7,I_8` and each of these intervals is assigned a symbol such as `[	1	2	3	4	5  6   7 8]`

Let, `x = 0.2848`. There is a mapping rule which maps `x` to the symbol depending on the interval in which `x` lies and we should obtain the same symbol elements as in `b`. The rule is 
 

    if x in I_1, assign symbol = 1 and  y= x/p(1);
    if x lies in I_2, assign symbol = 2 and y = (x-p(1))./p(2);
    if x lies in  I_3 , assign symbol = 3 y = (x-(p(1)+p(2)))./p(3);;
    if x lies in  I_4 , assign symbol = 4  y = (x-(p(1)+p(2)+p(3)))./p(4);;
    if x lies in  I_5 , assign symbol = 5 y = (x-(p(1)+p(2)+p(3)+p(4)))./p(5);
    if x lies in  I_6 , assign symbol = 6  y = (x-(p(1)+p(2)+p(3)+p(4)+p(5)))./p(6);
    if x lies in  I_7, assign symbol = 7 and y = (x-(p(1)+p(2)+p(3)+p(4)+p(5)+p(6)))./p(7)

where `y` is basically the next value of `x`. In this way, I will get an array of floating point numbers `x` and `symbols = b`.
I need to map the elements in `x` to these symbols using the intervals and obtain the value `y`. Theoretically, the value of `y` should be ` y = 0.2848 /0.4 =  0.7120` and the symbol = 1 since `x` is in `I_1`  . Then, based on this `y` value obtained, I find the next symbol. In this way I should get `symbol = b = [1,3,2,6,1]`.

 But, in my implementation I am getting infinity value for `y` and all incorrect results for symbols.  I am unable to understand where I am going wrong. Please help.      

    clear all
    
    N = 5;
    b = [1,3,2,6,1];
    
    [uniqueSym,~,idxUnq]=unique(b);
    pp = hist(b , uniqueSym);
    p = pp/sum(pp);
    Interval = cumsum([0 p]);
    
    
    p_1 = sum(b==1)/length(b);
    p_2 = sum(b==2)/length(b);
    p_3 = sum(b==3)/length(b);
    p_4 = sum(b==4)/length(b);
    p_5 = sum(b==5)/length(b);
    p_6 = sum(b==6)/length(b);
    p_7 = sum(b==7)/length(b);
    p_8 = sum(b==8)/length(b);
    
    p_arr = [p_1,p_2,p_3,p_4,p_5,p_6,p_7,p_8];
    
     
    
    x(1) = 0.2848;  
    [~,symbols(1)] = ObtainSymbols(x(1),p_arr,Interval);
    for k = 1:N
        [y,sym] = ObtainSymbols(x(k),p_arr,Interval);
        x(k+1) = y;
        symbols(k+1) = sym;
    end   

    function [y,sym] = ObtainSymbols(x,p,Interval)
     
    if (double(x)>=Interval(1)) && (double(x)<Interval(2))  %interval I1
        y= x/p(1);
        sym =  1;
       
    elseif (double(x)>=Interval(2)) && (double(x)<Interval(3)) %interval I2
        y = (x-p(1))./p(2);
        sym =  2; 
     elseif (double(x)>=Interval(3)) && (double(x)<Interval(4)) %interval I3
          y = (x-(p(1)+p(2)))./p(3);
          sym =  3; 
    elseif (double(x)>=Interval(4)) && (double(x)<Interval(5)) %interval I4
        y = (x-(p(1)+p(2)+p(3)))./p(4);
        sym =  4; 
    elseif (double(x)>=Interval(5)) && (double(x)<Interval(6)) %interval I5
       y = (x-(p(1)+p(2)+p(3)+p(4)))./p(5);
       sym =  5; 
    elseif (double(x)>=Interval(6)) && (double(x)<Interval(7)) %interval I6
        y = (x-(p(1)+p(2)+p(3)+p(4)+p(5)))./p(6);
        sym =  6;%interval I6
      
    else y = (x-(p(1)+p(2)+p(3)+p(4)+p(5)+p(6)))./p(7);
        symb = 7;
    end

  