## How can i solve the '??? Index exceeds matrix dimensions.' reply of the debugger?

on 3 May 2013

### Image Analyst (view profile)

I was writing a matlab prog. for plotting the codeword error probability for a C(7,4)block coding, and to compare the error prob.through computation by simulation and the one with analytically. for the first part(as shown below), it works fine. But, for the second part where i copied the same code for plotting the analytical codeword error probability graph in comparison with the computed error probability for wrong codewords <10 ,<100, and <1000 in the same graph, the '??? Index exceeds matrix dimensions' begins to happen starting from the wrong codewords <10. How could you help me to solve it?

***INPUT.TXT is a 4X1 column vector of elements [k 4 m 7] text file. and ***G_MARIX.TXT is a 4X7 matrix of elements [1 0 0 0 1 1 1;0 1 0 0 0 1 1;0 0 1 0 1 0 1;0 0 0 1 1 1 0] text file.

-------------------------------------------

***** Lately, even when i copy the examples in the matlab help on 'sum' eg. M = magic(3) on to the command window and when i compute sum(M), it replies me :

??? Index exceeds matrix dimensions.

I can't understand the problem. I have restarted the matlab prog. and my PC, but the problem still persists.

-----------------------------------------------------------------------

```clear all;
close all;
```
```fileID=fopen('INPUT.TXT');      %....opens the file filename for read access,
% and returns an integer file identifier.
n=fscanf(fileID, '%s', [4 1]);  %....reads and converts data from a text file into array A in column order.
k=str2num(n(2,1));              %....Vector A above is indexed in(row,column)pointing.
m=str2num(n(4,1));              % Here, number of information bits is k=4,
% and codeword length=7
codewordsNum=2^k;               %....number of code words.
V=de2bi([0:1:codewordsNum-1]);  %....all the possible information vectors,V.
C=V*G;
C=mod(C,2) ;    %....the corresponding codeword matrix.
```
```for i=1:1:2^k
W(i)=sum(C(i, :));
end
```
```for i=1:1:m+1                  %....for multiplicity.
A(i)=0;                    %....initial count
for j=1:1:2^k
if W(j)==i-1           %...hamming wt. of each(row)codeword.
A(i)=A(i)+1;       %...counting num. of codewords having i hamming wt.
end
end
end
%------------::::::  PART 1 ::::::::::::------------
```
```for s=0:1:8    %---------------*********Loop to vary the value of EbNo_dB.*******
EbNo_dB(s+1)=s;
EbNo_lin1=10^(EbNo_dB(s+1)/10);
Variance=0.5*(1/((k/m).*EbNo_lin1));
std_dev=sqrt(Variance);
wrongCW=0;
correctCW=0;
```
```while wrongCW < 100    %---------------------*** Erroneous codeword target**********
Vtx = randsrc(1,k,[1,0]);
Ctx=mod(Vtx*G,2);
for j=1:length(Ctx)
if Ctx(1,j)==0
S_BPSKed(1,j)= -1;
else
S_BPSKed(1,j)= +1;
end
end
for r=1:length(Rrx)
if Rrx(1,r) >= 0
Yrx(1,r)=1;                    % A BPSK Hard decision/demodulation is
else                             % performed on the noised code.
Yrx(1,r)=0;
end
end
%::::::::::::::ERROR PROBABILITY COMPUTATION BY SIMULATION::::::::::::::::
%--------------------------------------------------------------------------
for i=1:1:2^k
H_dist(i)=sum(Yrx~=C(i,:));        % to get the hamming distance between the received, Yrx vector
[d_min,position]=min(H_dist());   % and the 2^k rows of the codewords and then to correspond that codeword
Crx=C(position,:);                 %with minimum hamming distance to the received codeword vector.
end
if Crx==Ctx
correctCW=correctCW+1;
else
wrongCW=wrongCW+1;
end
end
errorProb_CW_simu(s+1)=wrongCW/(correctCW+wrongCW);  %--------codeword error probability(by simulation)
```
```end
%:::::::::::::::::::::END OF ERROR PROBABILITY COMPUTATION BY SIMULATION::::::::::::::::
%--------------------------------------------------------------------------
```
```%::::::::::::::::ANALYTICAL ERROR PROBABILITY COMPUTATION ::::::::::::::::
%--------------------------------------------------------------------------
for h=1:1:2^k
if W(h)~=0
W_non_zero(h-1)=W(h);
end
end
dmin=min(W_non_zero);
t=(floor(dmin-1)/2);
```
```   for f=0:1:8
Eb_No_dB(f+1)=f;
EbNo_lin=10^(Eb_No_dB(f+1)/10);
P=0.5*erfc(sqrt((k/m).*EbNo_lin));
summation=0;
for i=0:1:t
mCr=nchoosek(m,i);
sum=mCr.*P.^i.*(1-P).^(m-i);
summation=summation+sum;
end
errorProb_CW_analy(f+1)=1-summation; %------------codeword error probability( Analytically)  ```
```   end
%::::::::::::::END OF ANALYTICAL ERROR PROBABILITY COMPUTATION ::::::::::::::::
%---------------------------------------------------------------------```
```%---------------:::::::::::  PART 2  ::::::::---------------------------------
```
```%::::::::::::::N1<10,...ERROR PROBABILITY COMPUTATION BY SIMULATION::::::::::::::
```
```for s=0:1:8    %---------------*********Loop to vary the value of EbNo_dB.*******
EbNo_dB(s+1)=s;
EbNo_lin1=10^(EbNo_dB(s+1)/10);
Variance=0.5*(1/((k/m).*EbNo_lin1));
std_dev=sqrt(Variance);
wrongCW=0;
correctCW=0;
```
```while wrongCW < 10     %---------------------*** Erroneous codeword target**********
Vtx = randsrc(1,k,[1,0]);
Ctx=mod(Vtx*G,2);
for j=1:length(Ctx)
if Ctx(1,j)==0
S_BPSKed(1,j)= -1;
else
S_BPSKed(1,j)= +1;
end
end
for r=1:length(Rrx)
if Rrx(1,r) >= 0
Yrx(1,r)=1;                    % A BPSK Hard decision/demodulation is
else                             % performed on the noised code.
Yrx(1,r)=0;
end
end
%--------------------------------------------
for i=1:1:2^k
H_dist(i)=sum(Yrx~=C(i,:)); %<<<<<<<<<*** The problem is here!!!!!!!!      % to get the hamming distance between the received, Yrx vector
[d_min,position]=min(H_dist());   % and the 2^k rows of the codewords and then to correspond that codeword
Crx=C(position,:);                 %with minimum hamming distance to the received codeword vector.
end
if Crx==Ctx
correctCW=correctCW+1;
else
wrongCW=wrongCW+1;
end
end
errorProb_CW_simuN1(s+1)=wrongCW/(correctCW+wrongCW);  %--------codeword error probability(by simulation)
```
```end
```
```%::::::::::::::N2<100,....ERROR PROBABILITY COMPUTATION BY SIMULATION::::::::::::::::
```
```for s=0:1:8    %---------------*********Loop to vary the value of EbNo_dB.*******
EbNo_dB(s+1)=s;
EbNo_lin1=10^(EbNo_dB(s+1)/10);
Variance=0.5*(1/((k/m).*EbNo_lin1));
std_dev=sqrt(Variance);
wrongCW=0;
correctCW=0;
while wrongCW < 100     %---------------------*** Erroneous codeword target**********
Vtx = randsrc(1,k,[1,0]);
Ctx=mod(Vtx*G,2);
for j=1:length(Ctx)
if Ctx(1,j)==0
S_BPSKed(1,j)= -1;
else
S_BPSKed(1,j)= +1;
end
end
for r=1:length(Rrx)
if Rrx(1,r) >= 0
Yrx(1,r)=1;                    % A BPSK Hard decision/demodulation is
else                             % performed on the noised code.
Yrx(1,r)=0;
end
end
%---------------------------------------------
for i=1:1:2^k
%    H_dist(i)=sum(Yrx~=C(i,:));        % to get the hamming distance between the received, Yrx vector
%    [d_min,position]=min(H_dist());   % and the 2^k rows of the codewords and then to correspond that codeword
Crx=C(position,:);                 %with minimum hamming distance to the received codeword vector.
end
if Crx==Ctx
correctCW=correctCW+1;
else
wrongCW=wrongCW+1;
end
end
errorProb_CW_simuN2(s+1)=wrongCW/(correctCW+wrongCW);  %--------codeword error probability(by simulation)
```
```end
```
```%--------------------------------------------------------------------------
%::::::::::::::::::::::::::::::PLOTS OF GRAPHS:::::::::::::::::::::::::::::
%--------------------------------------------------------------------------
%--PART 1.Plot for the comparison of Codeword Error Prob. by simulation and Analytically---
figure (1)
semilogy(EbNo_dB,errorProb_CW_simu,'k*--');
hold on;
semilogy(Eb_No_dB,errorProb_CW_analy,'ro--');
xlabel('E_b/N_0 (dB)');
ylabel('P_w(e), Codeword Error Probability');
title(' Comparison of P_w(e) analytically and by simulation for WrongCW<100');
legend('Codeword Error Prob. by simulation','Codeword Error prob. analytically','Location','NorthEast');
grid on;
hold off;
%------------------------------------------------------------
%---PART 2. Plot for comparing Codeword error prob. Analytically and by simulation for
% different Wrong codeword values, N<10, N<100.
%  figure (2)
% semilogy(Eb_No_dB,errorProb_CW_analy,'ro--');
% hold on;
% semilogy(EbNo_dB,errorProb_CW_simuN1,'k*--');
% hold on;
% semilogy(EbNo_dB,errorProb_CW_simuN2,'k*--');
```
```% xlabel('E_b/N_0 (dB)');
% ylabel('P_w(e), Codeword Error Probability');
```
```--------------------------------------------------------------
```

per isakson

### per isakson (view profile)

on 4 May 2013

Are you saying that

```    m = magic( 3 );
s = sum( m ); ```

causes an "??? Index exceeds ..." error?

Nasir

### Nasir (view profile)

on 4 May 2013

Yes, but this is just for testing the matlab 'sum' function. when i tried to check it out, it gives me the same error--"??? Index exceeds matrix dimensions."

even, now after your comment. Here is my trial:

```>> m=magic(3)
```
```m =
```
```       8     1     6
3     5     7
4     9     2```
```>> s=sum(m)
??? Index exceeds matrix dimensions.
```
```>>
```

-----------------------------------------------

The reason why I bring the matlab 'sum' function is that my code's problem happens when assigning the value of the sum of the elements of a 1X7 bits vector by a 16X7 bits matrix(to find the hamming distance between each of the 16 rows of the matrix and the single 1X7 vector, so that i will have 16 values for the hamming distance). The problem is in saving or storing or assigning these values.----- In my first post, i tried to point where the problem occurs, which is:

```.
.
.
.
```
```%--------------------------------------------
for i=1:1:2^k
H_dist(i)=sum(Yrx~=C(i,:)); %<<<<<<<<<*** The problem is here!!!!!!!!      % to get the hamming distance between the received, Yrx vector
[d_min,position]=min(H_dist());   % and the 2^k rows of the codewords and then to correspond that codeword
Crx=C(position,:);                 %with minimum hamming distance to the received codeword vector.
end
if Crx==Ctx
correctCW=correctCW+1;
else
wrongCW=wrongCW+1;
end
end
```
```.
```
```.
```
```.
```

## Products

No products are associated with this question.

### Image Analyst (view profile)

on 4 May 2013

I think you've redefined sum(). It thinks it's an array rather than the built-in function. In the command window (before it errors), try this:

```whos sum
which -all sum
```

Tell us what it says.

Walter Roberson

### Walter Roberson (view profile)

on 4 May 2013

The line that redefines it is

```sum=mCr.*P.^i.*(1-P).^(m-i);
```
Nasir

### Nasir (view profile)

on 4 May 2013

Thank you! Finally, i have changed the variable 'sum' in sum=mCr.*P.^i.*(1-P).^(m-i); to avoid the redefinition ............keep it up!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test