0

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!

Siakon
  • 13
  • 2
  • 9
  • Also see [this question](http://stackoverflow.com/questions/20054047/subscript-indices-must-either-be-real-positive-integers-or-logicals-generic-sol) for [the generic solution to this problem](http://stackoverflow.com/a/20054048/983722). – Dennis Jaheruddin Nov 27 '13 at 15:44

2 Answers2

1

There is an assignment xq(i) = x(i) somewhere in the middle of your function, but you pass A as x from outside where you calculate A from t which is sampled by randn, so you can't promise xq is an integer.

s.bandara
  • 5,636
  • 1
  • 21
  • 36
  • I don't see any `unique` in your code, and the input is randomized. Can you run the code above again and `disp(xq)` for me right before you assign `D(w_count)`? – s.bandara Jan 15 '13 at 07:05
0

I'm not sure exactly what you are aiming to do, but your vector xq does not contain integers, it contains doubles. If you want to use a vector of indices as you do with centers(xq), all elements of the vector need to be integers.

Upon a little inspection, it looks like xq are x values, you should find some way to map them to the integer of the closest cell to which they belong (i'm guessing 'centers' represents centers of cells?)

RussH
  • 329
  • 2
  • 17
  • xq contains integers, when i try it manually to ans = centers(xq) works like charm. This is a non uniformity quantizer for the signal A. The xq is the produced signal after the quantize, so if i put unique(xq) the reusults are 1,2,3,4. I am geting the results from the first while loop since i commended the while loop and the D. – Siakon Jan 15 '13 at 06:44
  • No, as your code is written `xq` is constructed largely from the vector `x`, which is the `A` you create in your lines of input. `A` is not a vector of integers. Try editing your code to show `Nq(1:10)` the line before your error and you will see what I mean. If you get all integer values the code you are running is not the same as the code you posted. – RussH Jan 15 '13 at 07:05
  • I used the disp(xq) right before the D assignment( commended the D and the while loop to run it). I got the xq having values from 1 to 4 all integers. The thing is if you notoced i am reassign xq after the xq(i) = x(i) with a integer cnt. cnt can only take values from 1 to 4, but if it can take more still it will be an integer since i am incresing it by 1 only. – Siakon Jan 15 '13 at 07:09
  • Uncomment the `D` line and the while loop and look again. – RussH Jan 15 '13 at 07:11
  • Glad to hear. On Stackoverflow if your question is resolved it is helpful to click the checkmark so others can see what answered your problem. – RussH Jan 15 '13 at 07:23