add value from single element of array
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
the cyclist
2022-7-10
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?
回答(1 个)
the cyclist
2022-7-10
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!