how can I change variable value in a for loop?
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi!
i'm writing a code in which there is this part (indicated with "from here" to here" in the code below):
There i'm calculating S(h,:) as the 10% (0.1) of SP + delta. I would like also to calculate the 0.4 and 0.5 respectively, and place them in S(h+1) and S(h+2) (see figure).
I attached the data as xlsx file.
clear; close; clc;
LAT = 42;
SM = 150;
[filename, filepath] = uigetfile( '*.xls*' ) ;
data = xlsread( fullfile( filepath, filename ), 'data' ) ;
assert (size(data, 2) == 4, ...
'Input data must have exactly five columns.');
load('k')
Tm = data(:,3);
ind = k(1,:) == LAT;
k = [k(2:end,1) k(2:end,ind)];
im = (Tm / 5).^ 1.514;
k1 = unique(data(:,1));
for j = 1:numel(k1)
index = (data(:,1) == k1(j));
I = im(index);
I = sum(I);
Iyear{j} = [I];
end
Iyear = cat(1, Iyear{:});
Iyear1 = [k1 Iyear];
for i=1:length(data)
data(i,5)=k(k(:,1)==data(i,2),2);
data(i,6)=Iyear1(Iyear1(:,1)==data(i,1),2);
data(i,6)= round(data(i,6),1);
end
a = (675 * 1E-9 * (data(:,6).^3)) - (771 * 1E-7 * (data(:,6).^2)) + (1792 * 1E-5 * data(:,6)) + 0.49239;
a = round(a,2);
PET = 16 * data(:,5) .*(((10 * data(:,3)) ./ data(:,6)).^ a);
PET = round(PET(:,1),1);
PET(PET<0)=0;
P = data(:,4);
delta = P - PET;
delta = real(delta);
m =[0.1; 0.4; 0.5];
for h = 1:numel(Tm)
if Tm (h) >= -1
S(1,:) = P(1,:) -PET(1,:);
if delta(h) < 0
S(h,:)=0;
if h == 1
ST(h,:)= SM - (SM .* (1- exp(-(PET(h)- P(h))./SM)));
AET(h,:)= PET(h);
else
ST(h,:)= ST(h-1) - (ST(h-1) .* (1- exp(-(PET(h)- P(h))./SM)));
AET(h,:)= P(h) + (ST(h-1) .* (1- exp(-(PET(h)- P(h))./SM)));
end
else
if h == 1
ST(h,:)= SM;
AET (h,:) = PET(h);
else
if delta(h) < (SM - ST(h-1))
ST(h,:)= ST(h-1) + delta(h);
S(h,:)=0;
AET (h,:) = PET(h);
else
ST(h,:)= SM;
AET (h,:) = PET (h);
if ST(h-1) <= SM;
S(h,:) = delta (h)-(SM-ST(h-1));
else
%see from here
search_from = max(1, h-1000);
group_start_idx = find(Tm(search_from:h-1) > -1, 1, 'last') + search_from; %here
group_end_idx = find(Tm(search_from:h-1) < -1, 1, 'first')+ search_from; %here I try to determine the position where Tm is <-1 going backwards
if Tm(group_end_idx,:) >0
SP(h,:) = P(group_start_idx,:);
for ii = 1:length(m)
S(h,:) = SP(h,:)*m(ii) + delta(h,:)
S(h,:) = real(S(h,:));
end
ST(h,:) = SM + delta(h,:);
ST(h,:) = real(ST(h,:));
else
SP(h,:) = P(group_start_idx,:) + P(group_end_idx,:);
for ii = 1:length(m)
S(h,:) = SP(h,:)*m(ii) + delta(h,:)
S(h,:) = real(S(h,:));
end
ST(h,:) = SM + delta(h-1,:);
ST(h,:) = real(ST(h,:));
end
% ..to here
end
end
end
end
else
if h == 1
ST(h,:) = SM;
AET(h,:) = 0;
S(h,:) = 0;
else
ST(h,:) = ST(h-1) + delta(h);
AET(h,:) = 0;
S(h,:) = 0;
end
end
end
a = real(a);
data = real(data);
PET = real(PET);
delta = real(delta);
AET = real(AET);
ST = real(ST);
S = real(S);
댓글 수: 6
채택된 답변
darova
2020년 3월 21일
Try this
m =[0.1; 0.4; 0.5];
for ii = 1:length(m)
S(h,:) = SP(h,:)*m(ii) + delta(h,:)
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Animation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!