how can i solve this 'Undefined function or variable '?'.

조회 수: 3 (최근 30일)
mathapelo mokgoatsane
mathapelo mokgoatsane 2018년 4월 12일
편집: Walter Roberson 2018년 5월 8일
i get this error 'Undefined function or variable' however i defined it. how do i solve it? i think i should insect bounds, that will tell my code to stop is all nodes are in a cluster and continue when (remainder) there are still nodes remain that are not in a cluster, but i am failing to come up with a working code. please help me
here is my code
N=50 % number of nodes
Dreq=2, dij=distance matrix with rand no. from 1-10
%%%%%%%%%Cluster_9 %%%%%%%%%%
kk = 0;
k = 0;
for v = 1:length(remainder_7)
if dij(node_k,remainder_7(v))<= Dreq
k = k+1;
cluster_9(k)= remainder_7(v);
else kk = kk+1;
remainder_8(kk)= remainder_7(v); % remaining nodes that are not visited
end
end
for k =1:length(cluster_9)
for k1=1:length(cluster_9)
dij9(k,k1)=dij(cluster_9(k),cluster_9(k1)); % distances in cluster_9
end
end
candidate_n8(1)=node_k;
Lavg9(1)= Lavg(node_k);
k=0;
for n = 1:length(cluster_9)
if cluster_9(n)~=node_k
if max(dij9(n,:))<=Dreq && Lavg(cluster_9(n))<Lavg(node_k)
k=k+1;
candidate_n8(k)=cluster_9(n);
Lavg8(k)=Lavg(cluster_9(n));
end
end
end
[~,index]=min(Lavg9);
node_n = candidate_n8(index);
Controller(9)=node_n;
% finding the node in the remainder that is near the cluster_9
k = 0;
for k1 =1:length(cluster_9)
for k2 =1:length(remainder_8)
k = k+1;
DIJ_8(k) = dij(cluster_9(k1),remainder_8(k2));
TABLE(k,1) = k2;
end
end
[~,index] = min(DIJ_8);
node_k = remainder_8(TABLE(index,1));
%%%%%%%%%%Clister_10%%%%%%%%%%
kk = 0;
k = 0;
for v = 1:length(remainder_8)
if dij(node_k,remainder_8(v))<= Dreq
k = k+1;
cluster_10(k)= remainder_8(v);
else kk = kk+1;
remainder_9(kk)= remainder_8(v); % remaining nodes that are not visited
end
end
for k =1:length(cluster_10)
for k1=1:length(cluster_10)
dij10(k,k1)=dij(cluster_10(k),cluster_10(k1)); % distances in cluster_10
end
end
candidate_n9(1)=node_k;
Lavg10(1)= Lavg(node_k);
k=0;
for n = 1:length(cluster_10)
if cluster_10(n)~=node_k
if max(dij10(n,:))<=Dreq && Lavg(cluster_10(n))<Lavg(node_k)
k=k+1;
candidate_n9(k)=cluster_10(n);
Lavg9(k)=Lavg(cluster_10(n));
end
end
end
[~,index]=min(Lavg10);
node_n = candidate_n9(index);
Controller(10)=node_n;
end
Undefined function or variable 'remainder_8'.
Error in my_code (line 518)
for k2 =1:length(remainder_8)
  댓글 수: 6
mathapelo mokgoatsane
mathapelo mokgoatsane 2018년 4월 12일
thank you @Adam
Adam
Adam 2018년 4월 12일
remainder_8 is only created in an else statement. Presumably in the case you are running your code never goes into the else and so the variable does not exist. If this is a valid scenario you should create an empty variable somewhere towards the top of your code e.g.
remainder_8 = [];
then
length( remainder_8 )
will return 0, though then the error will just move to this line:
node_k = remainder_8(TABLE(index,1));
where you again assume this variable exists.

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

답변 (2개)

Walter Roberson
Walter Roberson 2018년 4월 12일
for v = 1:length(remainder_7)
if dij(node_k,remainder_7(v))<= Dreq
k = k+1;
cluster_9(k)= remainder_7(v);
else
kk = kk+1;
remainder_8(kk)= remainder_7(v); % remaining nodes that are not visited
end
end
In that code if the else is never true then remainder_8 will never be defined.

Stephen23
Stephen23 2018년 4월 12일
편집: Stephen23 2018년 4월 12일
The problem is simple: remainder_8 does not exist, because your code never defines it. Think about what happens when all of the sampled dij values are <= Dreq:
for ...
if dij(node_k,remainder_7(v))<= Dreq
...
else ...
remainder_8(kk) = ...
end
end
then remainder_8 will simply never be defined. Solution: put this at the start of your code:
remainder_ = [];

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by