how can I change variable value in a for loop?

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 commentaires

darova
darova le 21 Mar 2020
I did my best but inderstood nothing. I tried but failed
Can you be more specific?
EM geo
EM geo le 21 Mar 2020
Modifié(e) : per isakson le 21 Mar 2020
Suppose to have m =[0.1; 0.4; 0.5]; I would like to run a for loop cycle using m as index for the loop in order to calculate S(h,:). In the code above you can see that I calculated S(h,:) only for 0.1.
In a new version I tried to insert this:
m =[0.1; 0.4; 0.5];
for ii = 0:1:m(1)
S(h,:) = SP(h,:)*m + delta(h,:)
end
EM geo
EM geo le 21 Mar 2020
@darova now i solved some of the problems... i'm working on the others..! thank you!!!!
darova
darova le 21 Mar 2020
What are the others. Describe, show
@ darova I modified the code above with the corrections I made thanks to you. Now the problem is that I need to determine the position of Tm < -1 going backwards from h position. Then I need to index the variable P with the positions of Tm <-1 and make a sum of P, storing it in a new variable called SP. Then I compute the 0.1, 0.4 and 0.5 of SP I wrote this part of code but it moves uncorrectly backwards to h position. You can see this part above. In detail it is:
for h= 1:numel(Tm)
search_from = max(1, h-1000); % I assume to go backwards to a big number of positions
group_start_idx = find(Tm(search_from:h-1) > -1, 1, 'last') + search_from; %--> here there is a problem
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,:) % 0.1, 0.4 and 0.5 * SP
S(h,:) = real(S(h,:));
end
ST(h,:) = SM + delta(h-1,:);
ST(h,:) = real(ST(h,:));
end
end
I hope i'm clear enough....
darova
darova le 22 Mar 2020
It's to complicated. Can you make a scheme or drawing?

Connectez-vous pour commenter.

 Réponse acceptée

darova
darova le 21 Mar 2020
Try this
m =[0.1; 0.4; 0.5];
for ii = 1:length(m)
S(h,:) = SP(h,:)*m(ii) + delta(h,:)
end

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by