我在MATLAB中创建了以下class Timesheet(models.Model):
owner = models.ForeignKey(User, on_delete = models.CASCADE)
title = models.CharField(max_length = 64, verbose_name = _("Title"))
start = models.DateTimeField(verbose_name = _("Start time"))
end = models.DateTimeField(verbose_name = _("End time"))
allDay = models.BooleanField(blank = True, default = False, verbose_name = _("All day"), help_text = _("If it's a full day work, just click 'Now' for start/end"))
week = models.IntegerField(verbose_name = "working week")
def __str__(self):
return "{}".format(self.title)
def save(self, *args, **kwargs):
if not self.pk:
self.week = datetime.datetime.now().isocalendar()[1]
if self.allDay:
self.start = datetime.datetime(year = datetime.datetime.today().year, month = datetime.datetime.today().month, day = datetime.datetime.today().day, hour=8, minute=00)
self.end = datetime.datetime(year = datetime.datetime.today().year, month = datetime.datetime.today().month, day = datetime.datetime.today().day, hour=17, minute=30)
super(Timesheet, self).save(*args, **kwargs)
Graph
我想在两个连续的节点之间添加节点。
我正在尝试以下方法
1。获取function dummyGraph()
tail = [1 2 3 4 5 6 6 7 8 9 10 10 12 13 14 15];
head = [2 3 4 5 6 7 12 8 9 10 15 11 13 14 15 16];
Graph = graph(tail,head)
plot(Graph)
NodeNumber = 1:16
Neighbor={};
for NodeIdx =NodeNumber
Neighbor{NodeIdx} = neighbors(Graph,NodeIdx);
end
end
中存在的所有节点的邻居节点。
2.在两个连续的邻居之间添加四个新节点。
我可以在代码中实现第一步。我找不到用于在现有节点之间添加节点的MATLAB命令。
MATLAB文档中提供了命令Graph
。但是,我不确定如何使用此命令来实现我的情况。
我想征求有关如何进行的建议
答案 0 :(得分:1)
这是解决方案,
function AddNodes()
tail = [1 2];
head = [2 3];
Graph = graph(tail,head)
NVertex = size(Graph.Nodes);
NVertex = NVertex(1);
%% Name Nodes
NNode = string(1:NVertex)';
Graph.Nodes.Name = cellstr(NNode);
%% Adding nodes
GraphEdges = table2cell(Graph.Edges(:,1));
NEdges = size(GraphEdges);
NEdges = NEdges(1);
Source=[];
Sink =[];
for edge = 1:NEdges
Graph = addnode(Graph,4);
source = GraphEdges{edge}(1);
sink = GraphEdges{edge}(2);
Graph = rmedge(Graph,source,sink);
Add4Musk = Graph.Nodes.Name(end-3:end);
TobeAdded = vertcat(source,Add4Musk,sink);
for node = 1:length(TobeAdded)-1
source = TobeAdded(node);
sink = TobeAdded(node+1);
Graph = addedge(Graph,source,sink);
end
end
%% Plot graph
plot(Graph,'Layout','subspace3')
很高兴知道是否还有其他方法可以完成此任务。