迭代打印matlab结构
Iteratively print matlab struct
我有一个具有以下结构的 matlab 结构
Tree:
feature : numerical value (= 1)
tru: numerical value (= 2)
gain: numerical value (= 3)
left: struct with left node
right: struct with right node
如何以树的形式打印出来?例如我想为根节点打印出:
node 1 feature 1, tru 2, gain 3
节点 2 和 3 分别包含来自 left
和 right
结构的数据,与根节点具有相同的结构。
我想使用我上面为节点 1 描述的相同格式递归打印整个树。
我可以通过
打印我的节点1
tree.feature
tree.tru
tree.gain
我不知道如何为所有节点递归打印它的子节点。
你应该能够有一个像这样的简单递归函数
function printinfo(node)
% Display the details from this node
disp(['Feature: ', num2str(node.feature), ...
'\nTru: ', num2str(node.tru), ...
'\nGain: ', num2str(node.gain)]);
% If any of feature, tru or gain aren't equal to -1, go into left and right
if node.feature ~= -1 || node.tru ~= -1 || node.gain ~= -1
printinfo(node.left)
printinfo(node.right)
end
end
最初使用
调用它
printinfo(tree)
然后每个节点调用各自right
和left
节点的函数,除非达到你在注释中描述的停止条件:
It will stop when the node features, tru and gain is -1
我有一个具有以下结构的 matlab 结构
Tree:
feature : numerical value (= 1)
tru: numerical value (= 2)
gain: numerical value (= 3)
left: struct with left node
right: struct with right node
如何以树的形式打印出来?例如我想为根节点打印出:
node 1 feature 1, tru 2, gain 3
节点 2 和 3 分别包含来自 left
和 right
结构的数据,与根节点具有相同的结构。
我想使用我上面为节点 1 描述的相同格式递归打印整个树。
我可以通过
打印我的节点1tree.feature
tree.tru
tree.gain
我不知道如何为所有节点递归打印它的子节点。
你应该能够有一个像这样的简单递归函数
function printinfo(node)
% Display the details from this node
disp(['Feature: ', num2str(node.feature), ...
'\nTru: ', num2str(node.tru), ...
'\nGain: ', num2str(node.gain)]);
% If any of feature, tru or gain aren't equal to -1, go into left and right
if node.feature ~= -1 || node.tru ~= -1 || node.gain ~= -1
printinfo(node.left)
printinfo(node.right)
end
end
最初使用
调用它printinfo(tree)
然后每个节点调用各自right
和left
节点的函数,除非达到你在注释中描述的停止条件:
It will stop when the node features, tru and gain is -1