MATLAB: Index exceeds matrix dimensions (how to fix it )

:)'ro'2) hold onber(2index exceeds matrix dimensions. error in untitled2 (line 121) semilogy(snrlinewidth

hello and thank u all for reading and healping me
i do have a prblm with this code i didnt find the erreur + im sur that it's a small mistake wich i couldn't notice
—————————
Index exceeds matrix dimensions.
Error in Untitled2 (line 121)
semilogy(SNR,BER(2,:),'ro-' ,'LineWidth',2); hold on
%% ________________________________________________________________________
%% ________________________________________________________________________




clc; clear; close all;
%% ________________________________________________________________________
%% SIMULATION PARAMETERS __________________________________________________
par.B = 128; % NUMBER OF BASE-STATION ANTENNAS (B >> U)
par.U = 16; % NUMBER OF SINGLE-ANTENNA USERS
ModType = {'64QAM','BPSK', 'QPSK','8PSK','16QAM','64QAM'}; % MODULATION TYPE : 'BPSK', 'QPSK','8PSK'
% '16QAM','64QAM'
Precoders = {'ZF'}; % PRECODERS LIST {'MRT','ZF'}
NRuns = 1e3; % NUMBER OF MONTE-CARLO RUNS
SNR = -10:2:20; % SNR [dB]
%% ________________________________________________________________________
%% MAPPING PREPARATION ____________________________________________________
for o = 1:length(ModType)
switch (ModType{o})
case 'BPSK'
par.Symbols = [ -1 1 ];
case 'QPSK'
par.Symbols = [ -1-1i,-1+1i,+1-1i,+1+1i ];
case '8PSK'
par.Symbols = [ exp(1i*2*pi/8*0), exp(1i*2*pi/8*1), ...
exp(1i*2*pi/8*7), exp(1i*2*pi/8*6), ...
exp(1i*2*pi/8*3), exp(1i*2*pi/8*2), ...
exp(1i*2*pi/8*4), exp(1i*2*pi/8*5) ];
case '16QAM'
par.Symbols = [ -3-3i,-3-1i,-3+3i,-3+1i, ...
-1-3i,-1-1i,-1+3i,-1+1i, ...
+3-3i,+3-1i,+3+3i,+3+1i, ...
+1-3i,+1-1i,+1+3i,+1+1i ];
case '64QAM'
par.Symbols = [-7-7i,-7-5i,-7-1i,-7-3i,-7+7i,-7+5i,-7+1i,-7+3i, ...
-5-7i,-5-5i,-5-1i,-5-3i,-5+7i,-5+5i,-5+1i,-5+3i, ...
-1-7i,-1-5i,-1-1i,-1-3i,-1+7i,-1+5i,-1+1i,-1+3i, ...
-3-7i,-3-5i,-3-1i,-3-3i,-3+7i,-3+5i,-3+1i,-3+3i, ...
+7-7i,+7-5i,+7-1i,+7-3i,+7+7i,+7+5i,+7+1i,+7+3i, ...
+5-7i,+5-5i,+5-1i,+5-3i,+5+7i,+5+5i,+5+1i,+5+3i, ...
+1-7i,+1-5i,+1-1i,+1-3i,+1+7i,+1+5i,+1+1i,+1+3i, ...
+3-7i,+3-5i,+3-1i,+3-3i,+3+7i,+3+5i,+3+1i,+3+3i ];
end
end
%% ______________________________________________________________________

%%
% compute symbol energy
par.Es = mean(abs(par.Symbols).^2);
% precompute bit labels
par.bps = log2(length(par.Symbols)); % number of bits per symbol
par.bits = de2bi(0:length(par.Symbols)-1,par.bps,'left-msb');
%% ________________________________________________________________________
%% PREALLOCATION __________________________________________________________
BER = zeros(length(Precoders),length(SNR)); % BER
EVM = zeros(length(Precoders),length(SNR)); % EVM
%% ______________________________________________________________________
% generate random bit stream (antenna x bit x trial)
bits = randi([0 1],par.U,par.bps,NRuns);
%% ________________________________________________________________________
%% MONTE-CARLO LOOP _______________________________________________________
for i = 1:NRuns
% DATA GENERATION (AS DECIMAL)
idx = bi2de(bits(:,:,i),'left-msb')+1;
% MAPPING (MODULATION)
s = par.Symbols(idx).';
% CHANNEL MATRIX GENERATION
H = sqrt(0.5)*(randn(par.U,par.B) + 1i*randn(par.U,par.B));
% NOISE VECTOR GENERATION
n = sqrt(0.5)*(randn(par.U,1) + 1i*randn(par.U,1));
%% ____________________________________________________________________
%% PRECODING ALGORITHMS LOOP __________________________________________
for j = 1:length(Precoders)
%% ________________________________________________________________
%% SNR LOOP _______________________________________________________
for k = 1:length(SNR)
% NOISE VARIANCE
N0 = 10.^(-SNR(k)/10);
% PRECODING TECHNIQUES
switch (Precoders{j})
% MRT
case 'MRT',[x, beta] = PRECODER_1_MRT(s, H);
% ZF
case 'ZF', [x, beta] = PRECODER_2_ZF(par, s, H);
end
% CHANNEL EFFECT AND NOISE
Hx = H*x;
y = Hx + sqrt(N0)*n;
% NORMALIZATION OF RECEIVED SIGNAL BY beta
shat = beta*y;
% SINGLE USER-DETECTION
[~,idxhat]= min(abs(shat*ones(1,length(par.Symbols)) ...
- ones(par.U,1)*par.Symbols).^2,[],2);
bithat = par.bits(idxhat,:);
% COMPUTE BER AND EVM
err = (idx~=idxhat);
BER(j,k) = BER(j,k)+...
sum(sum(bits(:,:,i)~=bithat))/(par.U*par.bps);
EVM(j,k) = EVM(j,k)+100*norm(shat - s)^2/norm(s)^2;
end % END OF NORMALIZED TRANSMIT POWER LOOP _____________________
end % END OF PRECODING ALGORITHMS LOOP ______________________________
end % END OF MONTE-CARLO LOOP _____________________________________________
%% ________________________________________________________________________

%% RESULTS NORMALISATION __________________________________________________
BER = BER/NRuns;
EVM = EVM/NRuns;
%% ________________________________________________________________________
%% PLOT RESULTS ___________________________________________________________
%% PLOT BER
figure(1)
semilogy(SNR,BER(1,:),'bo-' ,'LineWidth',2); hold on
semilogy(SNR,BER(2,:),'ro-' ,'LineWidth',2); hold on
% semilogy(SNR,BER(3,:),'go-' ,'LineWidth',2); hold on
% semilogy(SNR,BER(4,:),'ko-' ,'LineWidth',2); hold on
% semilogy(SNR,BER(5,:),'yo-' ,'LineWidth',2); hold on
% semilogy(SNR,BER(6,:),'mo-' ,'LineWidth',2);
% semilogy(SNR,BER(2,:),'rs--','LineWidth',2);
xlabel('SNR [dB]','FontSize',12)
ylabel('BER','FontSize',12);
axis([min(SNR) max(SNR) 1e-4 1]);
legend('MRT Precoder','ZF Precoder','location','NorthEast')
%% PLOT EVM
% figure(2)
% semilogy(SNR,EVM(1,:),'bo-' ,'LineWidth',2); hold on
% semilogy(SNR,EVM(2,:),'rs--','LineWidth',2);
% xlabel('EVM','FontSize',12)
% ylabel('BER','FontSize',12);
% %axis([min(SNR) max(SNR) 1e-4 1]);
% legend('MRT Precoder','ZF Precoder','location','NorthEast')
%% ________________________________________________________________________

Best Answer

  • In this case
    >> whos BER
    Name Size Bytes Class Attributes
    BER 1x16 128 double
    Haing single row only, how can you acess the 2 row
    semilogy(SNR,BER(2,:),'ro-' ,'LineWidth',2); hold on
    %................^..this is error, BER haing 1x16, you trying to access 2 row
    Correct one, may be
    semilogy(SNR,BER,'ro-' ,'LineWidth',2);
    >>whos SNR
    Name Size Bytes Class Attributes
    SNR 1x16 128 double
    try it the error will not occur again, on that line.