I have the following error in MATLAB:
??? Subscript indices must either be real positive integers or logicals.
Error in ==> Lloyd_Max at 74 D(w_count) = mean((x - centers(xq)).^2);
This is my code :
function [ xq,centers,D ] = Lloyd_Max( x,N,min_value,max_value )
%LLOYD_MAX Summary of this function goes here
%   Detailed explanation goes here
x = x';
temp = (max_value - min_value)/2^N;
count=1;
for j=0:temp:((max_value - min_value)-temp),
   centers(count) = (j + j + temp )/2;
   count = count + 1;
end
for i=1:length(centers),
   k(i) = centers(i); 
end
w_count = 0;
while((w_count < 2) || (D(w_count) - D(w_count - 1) > 1e-6))
    w_count = w_count + 1;
    count1 = 2;
    for i=2:(count-1),
       T(i) = (k(i-1) + k(i))/2;
       count1 = count1 +1 ;
    end
    T(1) = min_value;
    T(count1) = max_value; 
    index = 1;
    for j=2:count1,
        tempc = 0;
        tempk = 0;
        for k=1:10000,
            if(x(k) >= T(j-1) && x(k) < T(j))
                tempk = tempk + x(k);
                tempc = tempc + 1; 
            end
        end
        k(index) = tempk;
        k_count(index) = tempc;
        index = index + 1;
    end
    for i=1:length(k),
        k(i) = k(i)/k_count(i);
    end
    for i=1:10000,
        if (x(i) > max_value)
            xq(i) = max_value;
        elseif (x(i) < min_value)
            xq(i) = min_value;
        else
            xq(i) = x(i);
        end
    end
    for i=1:10000,
        cnt = 1;
        for l=2:count1,
           if(xq(i) > T(l-1) && xq(i) <= T(l))
               xq(i) = cnt;
           end
           cnt = cnt +1 ;
        end
    end
   D(w_count) = mean((x - centers(xq)).^2);
end
end
and i call it and have these inputs :
M = 10000
t=(randn(M,1)+sqrt(-1)*randn(M,1))./sqrt(2);
A= abs(t).^2;
[xq,centers,D] = Lloyd_Max( A,2,0,4 );
I tried to comment the while and the D, Results : I got the xq and the centers all normal, xq in the 1-4 range, centers 1-4 indexes and 0.5-3.5 range.
I dont know whats going wrong here...Please help me.
Thank in advance!
MYSTERY SOVLED!
Thank you all guys for your help! I just putted out of the while the for loop :
for i=1:10000,
        if (x(i) > max_value)
            xq(i) = max_value;
        elseif (x(i) < min_value)
            xq(i) = min_value;
        else
            xq(i) = x(i);
        end
    end
and it worked like charm.... this loop was initilizing the array again. Sorry for that. Thank you again!