# How to fix index in position 2 exceeds array bounds (must not exceed 1920)

22 views (last 30 days)
Kelsey O'Donnell on 29 Jul 2022
Answered: Cris LaPierre on 30 Jul 2022
The error I receive is :
Index in position 2 exceeds array bounds. Index must not exceed 1920.
Error in firstpass (line 46)
if IN(jj+N/2,ii+M/2)~=1
My code:
%
% This function is used in conjunction with the MULTIPASS.M run-file.
% Inputs are allocated from within MULTIPASS.
% 1999 - 2001, J. Kristian Sveen (jks@math.uio.no)
% For use with MatPIV 1.5, Copyright
% Distributed under the terms of the GNU - GPL license
% timestamp: 21.20, 20 Feb 2001
if length(N)==1
M=N;winsize=[M N];
elseif length(N)==2
M=N(1); N=N(2); winsize=[M N];
end
overlap=ol; [sy,sx]=size(A);
if nargin < 6 | isempty(idx) | isempty(idy)
idx=zeros(floor(sy/(N*(1-ol))),floor(sx/(M*(1-ol))));
idy=zeros(floor(sy/(N*(1-ol))),floor(sx/(M*(1-ol))));
end
x=zeros(ceil((size(A,1)-N)/((1-overlap)*N))+1, ...
ceil((size(A,2)-M)/((1-overlap)*M)) +1);
y=x; u=x; v=x;
% change . october 2001, weight matrix added.
% W=weight('cosn',[M N],100);
if nargin==8,
end
else
IN=zeros(size(A));
end,
elseif nargin<8
IN=zeros(size(A));
end
cj=1;tic
for jj=1:((1-ol)*N):sy-N+1
ci=1;
for ii=1:((1-ol)*M):sx-M+1
if IN(jj+N/2,ii+M/2)~=1
##### 2 CommentsShowHide 1 older comment
Kelsey O'Donnell on 29 Jul 2022
It is a part of my runpiv code which is part of MatPIV, a code for running particle image velocimetry analysis. The code is attached. I first define my world coordinates with a scale image then create a mask for the images I want to process over areas that should be ignored by the processer.

Cris LaPierre on 30 Jul 2022
The error message indicates that you are trying to index your variable using a value that exceeds the size of the array. Here, position 2 means the value you are using to index the columns of IN. Specifically, there are 1920 columns, and the value your code is returning for ii+M/2 is >1920.
For example:
A = 1:5; % variable with 1 row, 5 columns
% This works
A(1,3)
ans = 3
% This generates the same error message you are getting
A(1,6)
Index in position 2 exceeds array bounds. Index must not exceed 5.

R2022a

### Community Treasure Hunt

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

Start Hunting!