Matlab错误:下标索引必须是实正整数或逻辑。

时间:2021-01-19 23:06:52

I have the following error in MATLAB:

我在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);

错误在74 D = = > Lloyd_Max(w_count)=意味着((x -中心(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.

我试着对while和D进行注释,结果:我得到xq和中心都是正常的,xq在1-4范围内,中心1-4个指标,0.5-3.5范围。

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 :

谢谢大家的帮助!我刚从while循环中抽出来:

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!

它曾像魅力....这个循环再次初始化数组。对不起。再一次感谢您!

2 个解决方案

#1


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.

在你的函数中间有一个任务xq(i) = x(i),但你把A作为x从外面算出来,你从t中计算出一个由randn抽样的t,所以你不能保证xq是一个整数。

#2


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.

我不确定你的目标是什么,但是你的向量xq不包含整数,它包含了双打。如果你想要使用一个与中心(xq)相关的索引向量,那么向量的所有元素都必须是整数。

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?)

在一个小的检查中,它看起来像xq是x的值,你应该找到一些方法把它们映射到它们所属的最近的单元的整数(我猜“中心”代表细胞的中心?)

#1


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.

在你的函数中间有一个任务xq(i) = x(i),但你把A作为x从外面算出来,你从t中计算出一个由randn抽样的t,所以你不能保证xq是一个整数。

#2


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.

我不确定你的目标是什么,但是你的向量xq不包含整数,它包含了双打。如果你想要使用一个与中心(xq)相关的索引向量,那么向量的所有元素都必须是整数。

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?)

在一个小的检查中,它看起来像xq是x的值,你应该找到一些方法把它们映射到它们所属的最近的单元的整数(我猜“中心”代表细胞的中心?)