With your current code you are comparing arrays of data. If I take a simple example with two randomly fillled arrays
t.example = rand(5,1);
t.compare = rand(5,1);
t.example
t.compare
t.example > t.compare
You can see that some match the comparison and some don't. If I put this comparison in an if statement I assume the if will only look at the first value of the array, thus you will always have the same outcome in your loop because you compare the same arrays of data at every step.
To fix your loop you can index your data array inside your for loop and run over all your data:
for i_loop=1:1:num_rows
if t.PowerSum(i_loop)>0
if t.SurplusPower(i_loop)<-(t.PowerSum(i_loop))
t.Self_cons(i_loop)=0;
elseif t.SurplusPower(i_loop)<0
t.Self_cons(i_loop)= t.PowerSum(i_loop) -abs(t.SurplusPower(i_loop));
elseif t.SurplusPower(i_loop)>=0
t.Self_cons(i_loop)= t.PowerSum(i_loop);
else
t.Self_cons(i_loop)=0;
end
end
end
Or you can make a logic arrays of your condition and change your data accordingly:
t.new = zeros(5,1);
logic_mask = t.example > t.compare;
% put 1 at the indices where my condition is met
t.new(logic_mask) = 1;
t.new % We see that the values of t.new where the logic array logic_mask is one have been changed
% The second statement of your loop could be something like this:
logic_mask = t.SurplusPower(i_loop)<0;
t.Self_cons(logic_mask)= t.PowerSum(logic_mask) -abs(t.SurplusPower(logic_mask));
I hope this helps,
Johan
