add value from single element of array
4 views (last 30 days)
Show older comments
Hi. I am writting a code where i would like to add value from array. So I have one value BAT which is a number. I have an array the size of 35040x1. How can I use for loop to add one value per one iteration to increase BAT value?
So below I managed to calculate a value for bat which is one value. but then after that I dont know what to use to still use this value and go through an array
for jj = 1:length(W_br)
if jj == 1
max_kap=C*kap_bat;
bat(jj,1)=0.5*max_kap(jj);
bat(jj,1) = bat(jj,1) - W_br(jj)
end
if jj > 1 && jj < 5
bat(jj) = bat(jj) - W_br(jj)
end
end
2 Comments
the cyclist
on 10 Jul 2022
I don't quite understand your question.
Could you give a small example of an input (maybe with a smaller vector that only has a few elements) and the output you expect?
Answers (1)
the cyclist
on 10 Jul 2022
Your example is confusing to me, partly because I think you made some careless math errors. But, the way I could approach this is to calculate the cumulative consumption and production, and then just use vector math (not a for loop) to get the BAT value at every step. Something like this:
consumption = [0.08 0.08 0.16 0.00];
production = [0.07 0.00 0.58 0.06];
cumulativeConsumption = cumsum(consumption);
cumulativeProduction = cumsum(production);
BAT_initial = 7.6;
BAT = BAT_initial - cumulativeConsumption + cumulativeProduction
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!