在matlab中生成三叉树

时间:2015-07-21 11:54:32

标签: matlab tree nested-loops function-handle

我要做的是生成一系列模拟非重组三叉树结构的向量。这是我的代码:

function Trinomial_tree
S{1}(1) = 100;
w{1} = 1.4;
w{2} = 1.1;
w{3} = 0.7;
T = 2;
%Compiling the w's into a vector
w = [w{1}, w{2}, w{3}];
%Actual vector-content generation goes here, right now the k-allocation
%doesn't work as intended. In the second run with i=3, k seems to be
%fixed on 3 
%{
for i = 2:(T+1)
    S{i} = zeros(1, 3^(1i-1));
end
%}
for i = 2:(T+1)
    S{i} = Node(w, T, i, S{i-1});
end
display(S{1})
display(S{2})
display(S{3})
end

这是节点功能:

function [S] = Node(w, T, i, S_1)
%Compute the continuing node of a point
%Pre-allocation
S = zeros(1, 3^(i-1));
%Nested loop which generates the different nodes
for k = 1:(3^(i-2))
for j = 1:((3^T)-2):3
S(j) = S_1(k) * w(1);
S(j+1) = S_1(k) * w(2);
S(j+2) = S_1(k) * w(3);
end
end

我进行了各种测试,但总是以同样的问题结束。 node.m函数仅在时间t = 2时编辑行向量的前3个条目,但保留其他6个条目。在我看来,我在循环中犯了一个错误,它在时间t = 1时没有采用向量的下一个值。

我可能也只是让我难以置信地使问题过于复杂,而我忽略了一个更容易和更明显的解决方案。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

j Node j = 1:((3^T)-2):3 T = 2循环j = 1:7:3

j时,这相当于1。 因此,Traceback (most recent call last): File "setup.py", line 22, in <module> raise DistutilsPlatformError() distutils.errors.DistutilsPlatformError 的值只有`ShareOpenGraphObject object = new ShareOpenGraphObject.Builder() .putString("og:type", "mygame.life") .putString("og:title", "Sample Game") .putString("og:description", "sample game to publish story") .build(); ShareOpenGraphAction action = new ShareOpenGraphAction.Builder() .setActionType("mygame.ask") .putObject("life", object) .build(); ShareOpenGraphContent content = new ShareOpenGraphContent.Builder() .setPreviewPropertyName("life") .setAction(action) .build(); shareDialog.show(activity,content);` 。 (它将采用的下一个值是8,它大于3,因此循环停止。)