Index exceeds error in second run

1 回表示 (過去 30 日間)
Brinta Chowdhury
Brinta Chowdhury 2018 年 4 月 8 日
回答済み: Brinta Chowdhury 2018 年 4 月 8 日
Hello , I am writing this code. It runs first time perfectly but second time it shows the error:
Index exceeds matrix dimensions.
Error in tr1 (line 61)
child(i)=citi_no(I)
my code is :
clc;
clear all;
close all;
P1= [5 2 1 4 3];
P2= [4 5 1 2 3];
for i=1:5
c=1;
a=find(P1==i);
if a<5 && a>1
u=P1(a-1);
v=P1(a+1);
elseif a==5
u=P1(a-1);
v=P1(1);
else
u=P1(5);
v=P1(a+1);
end
edge_table{i}(c)=u;
edge_table{i}(c+1)=v;
c=c+1;
b=find(P2==i);
if b<5 && b>1
x=P2(b-1);
y=P2(b+1);
elseif b==5
x=P2(b-1);
y=P2(1);
else
x=P2(5);
y=P2(b+1);
end
if max(ismember(edge_table{i},x))==0
edge_table{i}(c+1)=x;
c=c+1;
end
if max(ismember(edge_table{i},y))==0
edge_table{i}(c+1)=y;
end
citi_no(i)=i;
list_size(i)=length(edge_table{i});
end
idx=find(list_size==max(list_size));
chose=randi(length(idx),1);
I=idx(chose);
child(1)=citi_no(I);
for i=2:5
citi_no(I)=[];
edge_table{I}=[];
edge_table=edge_table(~cellfun('isempty',edge_table));
for j=1:numel(edge_table)
if max(ismember(edge_table{j},I))==1
d=find(edge_table{j}==I);
edge_table{j}(d)=[];
end
new_size(j)=length(edge_table{j});
end
id=find(new_size==min(new_size));
Next=randi(length(id),1);
I=id(Next);
child(i)=citi_no(I)
end
I could not find the problem.
  1 件のコメント
per isakson
per isakson 2018 年 4 月 8 日
編集済み: per isakson 2018 年 4 月 8 日
My test ends with that error when the value of I is 3 and the length of citi_no is 2

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

回答 (1 件)

Brinta Chowdhury
Brinta Chowdhury 2018 年 4 月 8 日
Hey thanks. I found the problem just after I post the question here and fixed it already. Anyways thanks for your help.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by