How to solve this error index in position 1 exceeds array bounds?

Hi,
How I can correct this error?
Index in position 1 exceeds array bounds (must not exceed 128).
Error in Create_Obs_dataL (line 60)
synth = synth(1+t0_i:nt+t0_i,:);

 Accepted Answer

The error is that your indexing of rows (specifically nt+t0_i), is resulting in a number higher that the total number of rows in your matrix. Here's a simple example:
A = rand(2,1)
A = 2×1
0.8538 0.6416
% Works
A(2,1)
ans = 0.6416
% Your error
A(3,1)
Index in position 1 exceeds array bounds. Index must not exceed 2.

6 Comments

@Cris LaPierre How to overcome this error, this is my code
nt = 115;
f0 = 50;
wr = 2*pi*1;
t0 = 2/f0;
t0_i = floor(t0/dt);
Unrecognized function or variable 'dt'.
wav = Ricker(f0,t0, nt, dt).';
th = (0:25)*pi/180;
nt = length(wav);
nw = 2^nextpow2(nt);
nwr = (nw/2 +1);
dw = 2*pi/(nw*dt);
w = (0:nwr-1)*dw;
w(w<2)=2;
[THETA,W] = meshgrid(th,w);
%% Compute reflection coefficients
Rsde = abco(vp,vs,rho,qp,qs,THETA, wr, W);
%%
Rsde_fft = fft(Rsde,nw);
wav_fft = fft(wav,nw);
synth_fft = zeros(size(W));
% % Multiply positive frequencies
for i=1:nwr
synth_fft(i,:) = Rpp_fft(i,i,:)*wav_fft(i);
end
% Build negative frequencies
synth_fft(nwr+1:nw,:) = conj(synth_fft((nwr-1):-1:2,:));
synth=real(ifft(synth_fft));
synth = synth(1+t0_i:nt+t0_i,:);
The way to fix it is to make sure nt+t0_i is less than 128.
Currently, dt is not defined, so perhaps your code is not using the value for dt that you intended. This will impact the value of t0_i
dt is there dt = 0.001, dt is there, I just not mentioned
actuall, when my data point was nt = 260, it is working well. but as in new data points nt = 115, it started giving error
nt = 115;
f0 = 50;
t0 = 2/f0;
dt = 0.001;
t0_i = floor(t0/dt)
t0_i = 40
nt+t0_i
ans = 155
Apparently your matrix has 128 rows, and you are trying to access the 155th, hence the error. Your equation does not appear to scale correctly with nt. You need to figure out what the correct value should be, and adjust your math accordingly.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!