I am getting an Index in position 1 exceeds array bounds error

I am getting the following index error:
Index in position 1 exceeds array bounds (must not exceed 1).
Error in L_Individual_Correlation_With_Players>DispCorr (line 36)
MeanSI1 = nanmean(RSI1(1:22,:),1);
Error in L_Individual_Correlation_With_Players (line 8)
IndPhasesDispCorr = DispCorr(IndGameDispersion,PhasesTimeStamps,PhasesDuration)
I don't understand why this error is occuring. If I run a few sample points individually, the error doesn't occur. Any help would be greatly appreciated. I have attached the sample workspace.
Here is the script:
IndPhasesDispCorr = DispCorr(IndGameDispersion,PhasesTimeStamps,PhasesDuration);
function IndDispCorr = DispCorr(IndGameDispersion,Idx,Context)
IndDispCorr = zeros(size(length(Idx)));
count = 0;
for k = 1:length(Idx)
PhaseWindow = Idx(k)+(Context(k,1)*10);
if PhaseWindow < Idx(k)
t = PhaseWindow:Idx(k);
else
t = Idx(k):PhaseWindow;
end
M = IndGameDispersion(t,:);
RSI1 = corrcoef([M(:,1:22) nanmean(M(:,1:22),2)],'rows','pairwise');
Rx1 = corrcoef([M(:,23:44) nanmean(M(:,23:44),2)],'rows','pairwise'); %Last column is centroid coordinates
Ry1 = corrcoef([M(:,45:66) nanmean(M(:,23:44),2)],'rows','pairwise');
RSI1(RSI1 == 1) = NaN;
Rx1(Rx1 == 1) = NaN;
Ry1(Ry1 == 1) = NaN;
MeanSI1 = nanmean(RSI1(1:22,:),1);
MeanRx1 = nanmean(Rx1(1:22,:),1);
MeanRy1 = nanmean(Ry1(1:22,:),1);
CorrN = [MeanSI1,nanmean(MeanSI1(1:22),2),MeanRx1,nanmean(MeanRx1(1:22),2),MeanRy1,nanmean(MeanRy1(1:22),2)];
count = count + 1;
IndDispCorr(count,1:72) = CorrN;
end
end

8 Comments

I am getting an Index in position 1 exceeds array bounds error in my code line No. 39, I am a beginner(noob) kindly help me to resolve this issue:
here is my code:
candidate_Image=imread('hide.png');
M = 'secret.txt';
secret = fopen(M,'rb');
[M,L] = fread(secret);
[n,m] = size(candidate_Image);
m = m/3;
if (m*n*3<L)
msg = msgbox('your picture is too small','size error','error','modal');
pause(1);
if (ishandle(msg))
close(msg);
end
end
latest_data = candidate_Image;
count = 1;
for i = 1:m
for j = 1:n
for k = 1:3
latest_data(i,j,k)=candidate_Image(i,j,k)-mod(candidate_Image(i,j,k),2)+M(count,1);
if count == L
break;
end
count = count+1;
end
if count == L
break;
end
end
if (L == count)
break;
end
end
imwrite(latest_data,'encryptedImage.png', 'bmp');
CC = M;
count1 = 1; %BOLD STARTS HERE
for i = 1:m
for j = 1:n
for k = 1:3
CC(count1) = latest_data(i,j,k) - candidate_Image(i,j,k);
if count1 == 1
break;
end
count1 = count1+1;
end
if count1 == L
break;
end
end
if count1 == L
break;
end
end %BOLD ENDS HERE
in bold portion I have some error, that I couldn't found yet.
CC(count1) = latest_data(i,j,k) - candidate_Image(i,j,k);
if count1 == 1
break;
end
count1 = count1+1;
you break; before you increment the count, so you will never execute the incrementing line. So, count1 will always be 1, and you will never assign to CC(2) or higher.
[M,L] = fread(secret);
The default for fread() is double=>double . But your input image that you are hiding the data into, is very likely uint8. Are you sure you want to be treating the file as containing double precision?
i just pickup the code from some youtube video his code is:
[M,L] = fread(secret,'ubitl');
but when we write this instad of, the error will occurs that is:
Error using fread
Invalid precision.
Error in Encrypt (line 4)
[M,L] = fread(secret,'ubitl');
what can i do know.
You did not happen to copy the code correctly. It should be
[M,L] = fread(secret,'ubit1');
Notice that is ubit1 -- ubit followed by the digit 1, not ubit followed by lower-case L
p=app.EditField_3.Value
A=zeros(20)
for i=1:20
E(i)=app.UITable.Data{i,1} % getting bound error in this line plese help
A=strfind(p,E(i))
A1=p(A+1);
N=isletter(A1)
if N==1
app.UITable.Data{i,2}=1;
elseif N==0
app.UITable.Data{i,2}=A1
end
end
Why do you assume that the uitable has at least 20 rows?
for i = 1 : height(app.UITable.Data)
E(i) = app.UITable.Data{i, 1}

Sign in to comment.

Answers (3)

The error means you are trying to index into an array, but are using indices that exceed the size of the array.
These are errors you can investigate using MATLAB debugging tools.
The simple answer is that when k=295, RSI1 is a 1x1 with value NaN. The line of code giving the error is asking for rows 1:22, which don't exist.
There is a progression of issues here. Basically, when k==295, (Context(k,1)==0. The result is PhaseWindow == Idx(k), so t=PhaseWindow = 4129.
It looks like you wrote the code assuming Context(k,1) would always be greater than 0. You just need to add code to handle this scenario and you should be good.
When you do this:
MeanSI1 = nanmean(RSI1(1:22,:),1);
It's saying that RSI1 has only 1 row, not 22.

2 Comments

Nope, not without diving into it more. Why did you pick 22 in the first place? Are you certain that there should be exactly 22 rows and not just 1 or 10 or some other variable number of rows? If you want to average the entire array (all rows and columns) regardless of how many rows and columns there are, and get the mean value for each column, then you can do this
% Average down along (within) columns row-by-row (direction 1).
% In other words, get a mean for each column.
columnMeansSI1 = mean(RSI1, 1, 'omitnan');
% Average across (within) columns, row-by-row (direction 2).
% In other words, get a mean for each row.
rowMeansSI1 = mean(RSI1, 2, 'omitnan');

Sign in to comment.

implement wavelet sub band coding in images using MATLAB software.

1 Comment

Sorry, I am perhaps a bit distracted today. Could you explain more clearly how this will help someone avoid the problem with indexing out of range?

Sign in to comment.

Categories

Asked:

on 19 Dec 2018

Commented:

on 6 May 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!