フィルターのクリア

Could you please check my code?

1 回表示 (過去 30 日間)
hrushikesh kyathari
hrushikesh kyathari 2019 年 6 月 29 日
コメント済み: Walter Roberson 2019 年 6 月 30 日
R=[1,-0.5,-0.5];
for i=2:25
a = -0.50;
b = 0.50;
R = [R;i,(b-a).*rand(1,1) + a,(b-a).*rand(1,1) + a];
end
R=[R;26,0.5,0.5];
disp(R);
[D,I] = pdist2(R(:,(2:3)),R(:,(2:3)),'euclidean','Smallest',3);
D(1,:)=[];
I(1,:)=[];
disp(D);
disp(I);
Edge=zeros(52,3);
for i=1:26
%intersect is a function written correctly!
if intersect(R(i,2:3),R(I(1,i),2:3))==1
continue
else
Edge(i,1)=i;
Edge(i,2)=I(1,i);
Edge(i,3)=D(1,i);
end
end
for j=1:26
if intersect(R(j,2:3),R(I(2,j),2:3))==1
continue
else
Edge(j+26,1)=j;
Edge(j+26,2)=I(2,j);
Edge(j+26,3)=D(2,j);
end
end
disp(Edge);
Q=zeros(1,3);
for k=1:51
if Edge(k,:)==Q
Edge(k,:)=[];
end
end
disp(Edge);
The error I am receiving is:
Index in position 1 exceeds array bounds (must not exceed 42).
Error in ehe (line 39)
if Edge(k,:)==Q
  3 件のコメント
hrushikesh kyathari
hrushikesh kyathari 2019 年 6 月 29 日
編集済み: hrushikesh kyathari 2019 年 6 月 29 日
Whenever I run the code, I am getting a change in error. Sometimes it is
Index in position....exceed 42,36,40.
I feel something is wrong with the last for loop used to delete empty rows,cause when I remove the loop, the code runs perfectly . Any suggestions for that.
the cyclist
the cyclist 2019 年 6 月 29 日
I have run your code repeatedly, and get no error.
Do you have the problem on a fresh restart of MATLAB?

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2019 年 6 月 29 日
mask = any(Edge,2);
Edge = Edge(mask,:);
or
mask = ~any(Edge,2);
Edge(mask,:) = [];
or
for k=51:-1:1
if Edge(k,:)==Q
Edge(k,:)=[];
end
end
  2 件のコメント
hrushikesh kyathari
hrushikesh kyathari 2019 年 6 月 29 日
編集済み: hrushikesh kyathari 2019 年 6 月 29 日
Thank you, Mr Roberson, your answer has solved my problem. Could you please explain what is the difference between the two for loops, so that in future I can solve the bug myself.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by