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

Roger Stafford
Roger Stafford 2017년 4월 30일
편집: Roger Stafford 2017년 4월 30일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2017년 4월 30일

0 개 추천

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

댓글 수: 1

MJE
MJE 2017년 4월 30일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

MJE
2017년 4월 30일

편집:

2017년 5월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by