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 ExchangeCreating 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