在Matlab中将函数循环中的值存储在函数中 [英] Storing values from a loop in a function in Matlab
问题描述
我正在用Matlab编写一个函数来模拟中风病人住院时间。我很难存储我的输出值。
这是我的函数:
$ p $ function [] = losdf(age,strokeType,dest)
%用于确定卒中患者的住院天数
%t =入院后的时间(天);
%年龄=患者年龄;
%strokeType = 1. Haemorhagic,2.脑梗塞,3. TIA;
%dest = 5。死亡6.护理之家7.常住地;
alpha1 = 6.63570;
beta1 = -0.03652;
alpha2 = -3.06931;
beta2 = 0.07153;
theta0 = -8.66118;
theta1 = 0.08801;
mu1 = 22.10156;
mu2 = 2.48820;
mu3 = 1.56162;
mu4 = 0;
nu1 = 0;
nu2 = 0;
nu3 = 1.27849;
nu4 = 0;
rho1 = 0;
rho2 = 11.76860;
rho3 = 3.41989;
rho4 = 63.92514;
for t = 1:1:365
p =(exp(-exp(theta0 +(theta1。* age))));
如果strokeType == 1
initialstatevec = [1 0 0 0 0 0 0];
elseif strokeType == 2
initialstatevec = [0 1 0 0 0 0 0];
else
initialstatevec = [0 0(1-p)p 0 0 0];
end
lambda1 = exp(alpha1 +(beta1。* age));
lambda2 = exp(alpha2 +(beta2。* age));
Q = [ - (lambda1 + mu1 + nu1 + rho1)lambda1 0 0 mu1 nu1 rho1;
0 - (lambda2 + mu2 + nu2 + rho2)lambda2 0 mu2 nu2 rho2;
0 0 - (mu3 + nu3 + rho3)0 mu3 nu3 rho3;
0 0 0 - (mu4 + nu4 + rho4)mu4 nu4 rho4;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0];
Pt = expm(t./365.*Q);
Pt = Pt(strokeType,dest);
Ft = sum(initialstatevec。* Pt);
Ft
end
end
然后运行我的函数我使用:
losdf(75,3,7)
我想在图表中绘制从0到365天的Ft值。做这个的最好方式是什么?
我需要首先将数值存储在数组中吗?如果是这样,最好的方法是什么?
许多方法可以做到这一点,一种简单的方法是在循环中将每个数据点保存到一个向量中,并在退出循环后绘制该向量。
...
Ft =零(365,1); %预先分配Ft作为365个零的向量
(对于t = 1):365
...
Ft(t)= sum(initialstatevec。* Pt); %在索引t处,存储你的输出
...
end
plot(1:365,Ft);
I am writing a function in Matlab to model the length of stay in hospital of stroke patients. I am having difficulty in storing my output values. Here is my function:
function [] = losdf(age, strokeType, dest)
% function to mdetermine length of stay in hospitaal of stroke patients
% t = time since admission (days);
% age = age of patient;
% strokeType = 1. Haemorhagic, 2. Cerebral Infarction, 3. TIA;
% dest = 5.Death 6.Nursing Home 7. Usual Residence;
alpha1 = 6.63570;
beta1 = -0.03652;
alpha2 = -3.06931;
beta2 = 0.07153;
theta0 = -8.66118;
theta1 = 0.08801;
mu1 = 22.10156;
mu2 = 2.48820;
mu3 = 1.56162;
mu4 = 0;
nu1 = 0;
nu2 = 0;
nu3 = 1.27849;
nu4 = 0;
rho1 = 0;
rho2 = 11.76860;
rho3 = 3.41989;
rho4 = 63.92514;
for t = 1:1:365
p = (exp(-exp(theta0 + (theta1.*age))));
if strokeType == 1
initialstatevec = [1 0 0 0 0 0 0];
elseif strokeType == 2
initialstatevec = [0 1 0 0 0 0 0];
else
initialstatevec = [0 0 (1-p) p 0 0 0];
end
lambda1 = exp(alpha1 + (beta1.*age));
lambda2 = exp(alpha2 + (beta2.*age));
Q = [ -(lambda1+mu1+nu1+rho1) lambda1 0 0 mu1 nu1 rho1;
0 -(lambda2+mu2+nu2+rho2) lambda2 0 mu2 nu2 rho2;
0 0 -(mu3+nu3+rho3) 0 mu3 nu3 rho3;
0 0 0 -(mu4+nu4+rho4) mu4 nu4 rho4;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0];
Pt = expm(t./365.*Q);
Pt = Pt(strokeType, dest);
Ft = sum(initialstatevec.*Pt);
Ft
end
end
Then to run my function I use: losdf(75,3,7)
I want to plot my values of Ft in a graph from from 0 to 365 days. What is the best way to do this?
Do I need to store the values in an array first and if so what is the best way to do this?
Many ways to do this, one straightforward way is to save each data point to a vector while in the loop and plot that vector after you exit your loop.
...
Ft = zeros(365,1); % Preallocate Ft as a vector of 365 zeros
for t = 1:365
...
Ft(t) = sum(initialstatevec.*Pt); % At index "t", store your output
...
end
plot(1:365,Ft);
这篇关于在Matlab中将函数循环中的值存储在函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!