I am working on a Semi Markov model. In it my task is to find the interval transition probability which is a recursive procedure. The code that I used is below. For this to run the initial condition is F(0)=eye(3,3) matrix.
I am unable to call this initial value in a loop. Is the code that I have written is right? I need a proper suggestion.
Other data used is
C =
Columns 1 through 4
[3x3 double] [3x3 double] [3x3 double] [3x3 double]
Column 5
[3x3 double]
Y =
Columns 1 through 4
[3x3 double] [3x3 double] [3x3 double] [3x3 double]
Column 5
[3x3 double]
The code:
F=mat2cell(zeros(3,15),[3],[3,3,3,3,3])
for j=1:5
for m=1:j
if (j-m)==0
F{:,j}=eye(3,3)
end
F{:,j}=Y{:,j}+sum(C{:,m}*F{:,(j-m)})
end
end
答案 0 :(得分:2)
在F{:,j}
时覆盖j-m==0
时,您仍会尝试稍后访问F{:,(j-m)}
。它没有说"单元格内容索引必须大于0"或者"下标索引必须是正整数或逻辑。"? Matlab数组和单元格从1开始编制索引。
您可能需要类似
的内容if (j-m)==0
F{:,j}=eye(3,3);
else
F{:,j}=Y{:,j}+sum(C{:,m}*F{:,(j-m)});
end
别忘了你的分号,否则你会得到大量不必要的输出。
你真的需要细胞吗?您编写的内容可以使用多维数组轻松实现,在这种情况下,实际上需要冒号。在您的具体示例中,我认为它们是多余的,只需编写F{j-m}
即可。