我想要做的是生成一系列向量来模拟非重组三叉树的结构。这是我的代码:
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我做了各种各样的测试,但总是以同样的问题结束。M函数仅在time t=2中编辑行向量的前3个条目,而将其他6个条目排除在外。在我看来,我在循环中犯了一个错误,以至于它在time t=1上不接受向量的下一个值。
这也可能是我令人难以置信地过于复杂的问题,而有一个更简单和明显的解决方案,我忽略了。
任何帮助都将不胜感激。
发布于 2015-07-21 13:09:23
对于j循环的Node,您有j = 1:((3^T)-2):3。
当T = 2时,这相当于j = 1:7:3。因此,j将永远只具有1的值。(下一个值是8,大于3,因此循环停止。)
https://stackoverflow.com/questions/31538357
复制相似问题