I'm getting Index in position 2 exceeds array bounds error
Mostrar comentarios más antiguos
clc
clear all
close all
%% Input arguments
rec=load('data.mat');
received=cell2mat(struct2cell(rec));
x_meas=0:0.1:9.9;
temp=0:0.1:9.9;
theta_meas=atan(10./temp);
temp1=0:1:99;
beam_angle=atan(10./temp1);
Fs=60;
x_min=1;
x_max=10;
y_min=1;
y_max=10;
r_begin=0.4; % The range that corresponds to the first sample of the data
resolution=1; % Spatial resolution of the image in meters
% Initialization
c=0.2999; % Speed of light
% Information on the distances corresponding to samples
sample_spacing =c/Fs/2; % The distance between two samples .
x_size = floor((x_max-x_min)/resolution); % Number of pixels in the x- direction
y_size = floor((y_max-y_min)/resolution); % Number of pixels in the y- direction
% Initialize matrices
result=zeros(x_size,y_size); % Final Image Result
range_signal=result; % Matrix that describes which pixels are visible for the antenna
% Vectors with the x- and y- coordinates that correspond to the samples
x_pixel=linspace(x_min,x_max,x_size);
y_pixel=linspace(y_min,y_max,y_size);
% Min & max angles bepalen
theta_min=theta_meas-beam_angle; % Angle at which the beam starts with respect to horizontal
theta_max=wrapToPi(theta_meas+beam_angle); % Angle at which the beam ends with respect to horizontal
for i =1:length(x_meas) % Loop over the measurements
% Reset temporary matrix
range_signal=ones(x_size,y_size);
% Determine for a certain height , at which x- value the beam starts and determine which pixels are then not visible by the antenna before the beam .
if (theta_min(i)>0)
for j=1:y_size
x_theta_min=y_pixel(j)/tan(theta_min(i))+x_meas(i);
x_theta_min=min([x_theta_min,x_max]);
range_signal(:,j)=range_signal(:,j).*(x_pixel<= x_theta_min)';
end
end
% Determine for a certain height , at which x- value the beam ends and determine which pixels are then not visible by the antenna after the beam .
if (theta_max(i)>0)
for j =1: y_size
x_theta_max=y_pixel(j)/tan(theta_max(i))+x_meas(i);
x_theta_max=max([ x_theta_max,x_min]);
range_signal(:,j)=range_signal(:,j).*(x_pixel>=x_theta_max)';
end
end
% For all the pixels that are visible for the antenna
for xx=1:x_size
for yy=1:y_size
if range_signal(xx,yy) % ~= 0
% Calculate the distance between measurement position and pixel
distance_from_measurement=sqrt((x_pixel(xx)-x_meas(i))^2+y_pixel(yy)^2);
% Find the corresponding signal value with the help of interpolation and add this to the result
sample_actual=((distance_from_measurement-r_begin)/sample_spacing );
sample_actual_floor=max([floor(sample_actual),1]);
sample_actual_mod=mod(sample_actual,1);
result(xx,yy)=result(xx,yy)+(1-sample_actual_mod)*received(i,sample_actual_floor) +...
sample_actual_mod*received(i,(sample_actual_floor+1));
end
end
end
end
result
im getting the error in this line given below
result(xx,yy)=result(xx,yy)+(1-sample_actual_mod)*received(i,sample_actual_floor) +...
sample_actual_mod*received(i,(sample_actual_floor+1));
error is like this
Index in position 2 exceeds array bounds (must not exceed 4096).
Error in BPA (line 87)
(1-sample_actual_mod)*received(i,sample_actual_floor) +...
.. please help me
6 comentarios
KSSV
el 20 de Jun. de 2022
What is received?
Sowjanya Lanka
el 21 de Jun. de 2022
Image Analyst
el 21 de Jun. de 2022
What is the error? There is no bold line. Please give us ALL THE RED TEXT after you read this:
Once you know how to debug you'll know what the variable in second index is, and what its value is. This will help:
Jan
el 21 de Jun. de 2022
"in that bold line im getting the error" - there is no bold face in formatted code.
Sowjanya Lanka
el 21 de Jun. de 2022
Sowjanya Lanka
el 21 de Jun. de 2022
Editada: Sowjanya Lanka
el 21 de Jun. de 2022
Respuestas (1)
sanidhyak
el 6 de Feb. de 2025
Hi Sowjanya,
I too faced the same error when I replicated your code and ran it locally.
The error message: “Index in position 2 exceeds array bounds (must not exceed 4096).” indicates that “sample_actual_floor + 1” is exceeding the bounds of the “received” matrix. To fix this, it should be ensured that “sample_actual_floor + 1” does not exceed the number of columns in “received”.
Kindly refer to the following corrected part of the code:
for yy=1:y_size
if range_signal(xx,yy) % ~= 0
% Calculate the distance between measurement position and pixel
distance_from_measurement=sqrt((x_pixel(xx)-x_meas(i))^2+y_pixel(yy)^2);
% Find the corresponding signal value with the help of interpolation and add this to the result
% Ensure sample_actual_floor does not exceed the last index
max_samples = size(received, 2); % Get the number of columns (samples)
sample_actual = (distance_from_measurement - r_begin) / sample_spacing;
sample_actual_floor = max(1, min(floor(sample_actual), max_samples - 1)); % Ensure within bounds
sample_actual_mod = mod(sample_actual, 1); % Get the fractional part
% Interpolate signal value safely
result(xx,yy) = result(xx,yy) + ...
(1 - sample_actual_mod) * received(i, sample_actual_floor) + ...
sample_actual_mod * received(i, sample_actual_floor + 1);
end
end
Here, “max_samples = size(received, 2)” gets the total number of values. “sample_actual_floor = max(1, min(floor(sample_actual), max_samples - 1))” keeps it within a safe range so that “sample_actual_floor + 1” does not go out of limits. This would prevent the error and would also ensure proper calculation.
Cheers & Happy Coding!

Categorías
Más información sobre Environment and Clutter en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!