Matlab keeps saying this matrix is undefined?

function [Crash,D_min] = crash_func (t, x, P_S, P_N, N_S, N_N)
% D_min: minimum distance between NS and E cars
% x is position vector for a single E car
% P_S and P_N are position vectors for S and N cars
% N_S and N_N are number of S and N cars
[dangerT,dangerCol] = find(x>= -5 & x<= 5);
for i = 1:N_S-1
for j = 1:numel(dangerT)
d_ES(i,j) = find_d(x(j),0,-2, P_S(j,i));
end
end
for i = 1:N_N-1
for j = 1:numel(dangerT)
d_EN(i,j) = find_d(x(j),0,2, P_N(j,i));
end
end
d_ES = d_ES';
d_EN = d_EN';
mins = [min(d_ES),min(d_EN)];
D_min = min(mins);
% if minimum distance between cars is less than 2*radius
Crash = false;
if D_min < 3
Crash = true;
end
end
function [d] = find_d (x1,y1,x2,y2)
d = sqrt((y2-y1)^2+(x2-x1)^2);
end
I keep getting an error that says d_EN and d_ES are undefined when the lines d_ES = d_ES' and d_EN = d_EN' are run

1 Comment

I can only guess at the trouble, but if ‘find_d’ is attempting to find certain elements in P_S and P_N, and if it fails to do so or if it finds multiple copies, then d_ES and d_EN will not be properly defined everywhere.

Sign in to comment.

 Accepted Answer

dangerT might be empty so numel(dangerT) might be 0, so the "for j" loops might never be executed.

1 Comment

This seems to have fixed it. Since not every car entered the danger zone, dangerT was indeed empty at some points in the code. Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!