错误:A(:) = B,A和B中的元素数必须相同

时间:2017-11-18 04:42:53

标签: matlab

据我所知,ber矩阵与err不匹配。需要帮助来克服以下代码中的错误:

for snr= 1:50
err=0; 
ber = err;
No= 1/10^(snr/10);
    for i= 1: 10^6;
        x= sign (randn(2,1));
        h= randn (1,2) * sqrt(1/2) + sqrt (1/2)* 1j* randn(1,2);
        w= sqrt(No/2)* randn (1,2) + sqrt(No/2)*1j* randn(1,2);
        alamh=[ h(1) h(2); conj(h(2)) -conj(h(1))];
        y= alamh * x + transpose(w);
        h_pseudoinverse= inv(ctranspose(alamh)* alamh)\ ctranspose(alamh);
        r= x + h_pseudoinverse * transpose (w);
        est_x= sign(real(r));
        err= (est_x~=x)+err; 
    end        
ber(snr)= err/10^6;
end

semilogy (1:50, ber,'-O')

错误:

  

错误:A(:) = B,A和B中的元素数必须相同

2 个答案:

答案 0 :(得分:0)

内循环运行后,err为2x1矩阵。如果您想将err的每次迭代存储在ber中,那么您可以,例如:

(1)在代码之前添加行ber = NaN(2, 50);

(2)删除行ber = err;

(3)将行ber(snr)...更改为ber(:, snr) = err/10^6;

答案 1 :(得分:0)

你需要删除第三行'ber = err',因为在每个循环中你定义err = 0,然后你分配ber = err,这将使ber = 0.此外,在第14行,因为'est_x 〜= x'为您提供一个包含2个元素的列向量,您可以将它们相加以获得错误的总位数估计值。这是编辑过的代码:

            for snr= 1:50
            err=0; 
            % ber = err;
            No= 1/10^(snr/10);
                for i= 1: 10^6
                    x= sign (randn(2,1));
                    h= randn (1,2) * sqrt(1/2) + sqrt (1/2)* 1j* randn(1,2);
                    w= sqrt(No/2)* randn (1,2) + sqrt(No/2)*1j* randn(1,2);
                    alamh=[ h(1) h(2); conj(h(2)) -conj(h(1))];
                    y= alamh * x + transpose(w);
                    h_pseudoinverse= inv(ctranspose(alamh)* alamh)\ ctranspose(alamh);
                    r= x + h_pseudoinverse * transpose (w);
                    est_x= sign(real(r));
                    err= sum((est_x~=x))+err;
                end        
            ber(snr)= err/10^6;
            end

            semilogy (1:50, ber,'-O')