Index exceeds error in second run

조회 수: 2 (최근 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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by