我在mat实验室做一个项目,我正在处理大量数据。我需要从电子表格中为现有模型创建源代码,其中计算在电子表格中逐行完成。我在编写电子表格最后5列的代码时遇到问题,其中第一列第二行的结果正在进行使用第五列第一行的结果,依此类推。 有关信息,
用于清晰视图的excel文件的屏幕截图
这是我做的代码:
%% dh/dx
dh_dx=zeros(numel(rocky.RAIN),1);
% for the first row
dh_dx(1)=(ingwl-effecbase)/meandx;
%for the remaining dh/dx
for s=2:numel(HER);
dh_dx(s)=(REVGW(s-1)-effecbase)/meandx;
end
%% GW flow for unit width
GWFLOW=zeros(numel(rocky.RAIN),1);
for t=1:numel(HER);
GWFLOW(t)=K*dh_dx(t)*aqb;
end
%% Drop of GW due to GW flow
GWDROP=zeros(numel(rocky.RAIN),1);
for u=1:numel(HER);
GWDROP(u)=(GWFLOW(u)/(aqb*por));
end
%% Net Groundwater change
NETGWCHANGE=zeros(numel(rocky.RAIN),1);
for v=1:numel(HER);
NETGWCHANGE(v)=INCGW(v)-GWDROP(v);
end
% Revised GW level Estimate
REVGW=zeros(numel(rocky.RAIN),1);
%for the first row
REVGW(1)=ingwl+NETGWCHANGE(1);
for w=2:numel(HER);
REVGW(w)=REVGW(w-1)+NETGWCHANGE(w)
end
提前感谢
答案 0 :(得分:2)
问题是你在代码中分别计算所有数据,而需要在同一个循环中一起计算:
% Allocate data
dh_dx = zeros(numel(rocky.RAIN),1);
GWFLOW = zeros(numel(rocky.RAIN),1);
GWDROP = zeros(numel(rocky.RAIN),1);
NETGWCHANGE = zeros(numel(rocky.RAIN),1);
REVGW = zeros(numel(rocky.RAIN),1);
% Seed the calculation on the first row
dh_dx(1) = (ingwl-effecbase)/meandx;
GWFLOW(1) = K*dh_dx(1)*aqb;
GWDROP(1) = (GWFLOW(1)/(aqb*por));
NETGWCHANGE(1) = INCGW(1)-GWDROP(1);
REVGW(1) = ingwl + NETGWCHANGE(1);
% Loop trough the other rows and calculate fields
for s = 2:numel(HER)
dh_dx(s) = (REVGW(s-1)-effecbase)/meandx;
GWFLOW(s) = K*dh_dx(s)*aqb;
GWDROP(s) = GWFLOW(s)/(aqb*por);
NETGWCHANGE(s) = INCGW(s) - GWDROP(s);
REVGW(s) = REVGW(s-1) + NETGWCHANGE(s);
end;