Info

此问题已关闭。 请重新打开它进行编辑或回答。

Matrix dimension error while trying to eliminate an element in cell array

1 次查看(过去 30 天)
I have written a function loop3 which calls a function kshortestpath (By Meral Sh.). kshortestpath finds k paths (as cell array) between a given source and destination(with first one among them being the shortest path), and the corresponding costs. loop3 is included to iterate the function with only certain 'sources' called terminals and finally list all the paths found. I need to eliminate those paths which have costs more than 1.25 times the shortest path (between a source and destination)
function [v,w ,a,b s,t] = loop3(N)%N represents the total number of nodes in the network
v = [];
w=[];
NT = input('Enter the number of terminals');% to set the number of origins
kpaths = input('Enter the value of k');% number of shortest paths required
for terminal = 1:NT
sub= input('Enter the terminal number');
for x = sub
for y = 1:N
source = x;
destination = y;
if(source~=destination)
[a,b] = kShortestPath(netCostMatrix, source, destination,kpaths);
for i = 2:kpaths
if b(1,i) > (1.25 * b(1,1))
a{1, i }=[];
end
end
s = a';
t = b';
v = vertcat(v,s);
w = vertcat(w,t);
a=[];
b=[];
end
end
end
end
function [shortestPaths, totalCosts] = kShortestPath(netCostMatrix, source, destination, k_paths)
.............. % the code for kShortestPAth comes here......
When I run the code it shows an error:
Index exceeds matrix dimensions.
Error in loop3 (line 16)
if b(1,i) > (1.25 * b(1,1))
Can somebody help me find what is wrong with this code
  2 个评论
KSSV
KSSV 2016-10-26
[a,b] = kShortestPath(netCostMatrix, source, destination,kpaths);
In the above check the size of b, it shall be a array/ matrix. You tried to extract b(1,i) for i values greater then dimension of b.
Hari
Hari 2016-10-26
编辑:Hari 2016-10-26
Thanks. You are right. Error is displayed when b becomes a null matrix at a particular point in the iteration. Is there a way to improve the code and fix this?

回答(0 个)

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by