Please help me with this error!

1 次查看(过去 30 天)
W = [1 4 3 3 9 7 5 10 5 8 6];
a=[1 2 4 2 1 1 1 3 3 4 2];
b=[2 4 6 5 5 4 3 6 4 5 3];
DG = sparse(a,b,W);
% tril returns the lower triangular part of the matrix.
UG = tril(DG + (DG'));
h = view(biograph(UG,[],'ShowArrows','off','ShowWeights','on'));
[dist,path,pred] = graphshortestpath(UG,1,6,'Directed',false);
set(h.Nodes(path),'Color',[1 0.4 0.4])
fowEdges = getedgesbynodeid(h,get(h.Nodes(path),'ID'));
revEdges = getedgesbynodeid(h,get(h.Nodes(fliplr(path)),'ID'));
edges = [fowEdges;revEdges];
set(edges,'LineColor',[1 0 0]);
set(edges,'LineWidth',1.5);
I got the error : Error using +
Matrix dimensions must agree.
Error in tesing (line 10)
UG = tril(DG + (DG'));
Thank you!

采纳的回答

Walter Roberson
Walter Roberson 2021-5-1
W = [1 4 3 3 9 7 5 10 5 8 6];
a=[1 2 4 2 1 1 1 3 3 4 2];
b=[2 4 6 5 5 4 3 6 4 5 3];
mx = max(max(a),max(b));
DG = sparse(a,b,W,mx,mx);
% tril returns the lower triangular part of the matrix.
UG = tril(DG + (DG'));
h = view(biograph(UG,[],'ShowArrows','off','ShowWeights','on'));
[dist,path,pred] = graphshortestpath(UG,1,6,'Directed',false);
set(h.Nodes(path),'Color',[1 0.4 0.4])
fowEdges = getedgesbynodeid(h,get(h.Nodes(path),'ID'));
revEdges = getedgesbynodeid(h,get(h.Nodes(fliplr(path)),'ID'));
edges = [fowEdges;revEdges];
set(edges,'LineColor',[1 0 0]);
set(edges,'LineWidth',1.5);

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by