Tall column: Efficient way to limit tall array column value

9 views (last 30 days)
Dear all,
I want to create a new tall column with the following calculation.
batterycapacity(n) = batterycapacity(n-1) - consumed power + charging power.
batterycapacity and consumed power are both tall array columns. Charging power is a constant value.
One side note is that the battey capacity can not be more then the maximum battery capacity. (in this example 104kWh)
Could somebody give me a hand on how to set this up?
Thanks in advance.

Answers (1)

Satyam
Satyam on 16 Jun 2025
According to my understanding one needs to calculate a new tall column for battery capacity in MATLAB, using the recurrence relation:
batterycapacity(n) = batterycapacity(n−1) − consumedpower(n) + chargingPower
Here, both 'batterycapacity' and 'consumedpower' are tall arrays, while 'chargingPower' is a constant scalar. The battery capacity must also not exceed a defined maximum capacity.
However, due to the sequential dependency ('batterycapacity(n)' depends on 'batterycapacity(n-1)'), this operation cannot be directly performed on tall arrays using element-wise operations. Instead, the arrays must be gathered into memory for such recursive computation. One can leverage 'gather' function of MATLAB for that purpose. Refer to the documentation of 'gather' function to learn about its syntax and usage: https://www.mathworks.com/help/matlab/ref/tall.gather.html
Below is the MATLAB implementation of this approach:
% Given: tall arrays batterycapacity and consumedpower
% chargingPower is a constant scalar
% maxCapacity is the upper limit for battery capacity
chargingPower = 5; % Example charging power in kWh
maxCapacity = 104; % Maximum battery capacity in kWh
% Gather initial value and consumed power for sequential calculation
batteryInit = gather(batterycapacity(1)); % Initial battery level
consumedVec = gather(consumedpower); % Gather consumed power
% Initialize result vector
n = length(consumedVec);
batteryNew = zeros(n,1);
batteryNew(1) = batteryInit;
% Compute battery capacity recursively
for i = 2:n
batteryNew(i) = batteryNew(i-1) - consumedVec(i) + chargingPower;
batteryNew(i) = min(batteryNew(i), maxCapacity); % Clamp to max
end
% Convert back to tall array if needed
batteryNewTall = tall(batteryNew);
I hope it solves the query.

Community Treasure Hunt

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

Start Hunting!