MATLAB Answers

Assign values in a matrix

3 views (last 30 days)
My script considers charging of Electric Vehicles. The first column is when the EV starts charging (in what minute), the second how much is charged, the third how long it charges. Example
3849 1 4
I would like to add values so the matrix looks like this, the values in the third column is now not relevant as it is expressed in per minute
3849 0.25 1
3850 0.25 1
3851 0.25 1
3852 0.25 1
  2 Comments
Joel Schelander
Joel Schelander on 8 Mar 2021
The car is charged 1 kWh at time 3849 for 4 minutes. I want to express what happens each minute
So what I want is to make new rows (in this case 4 since the charging lasts for 4 minutes) and divide the charging equally between the new rows. The third column is then not important as it is described now as number of rows.
Hope this clears things out, cause I would really appreciate help with this

Sign in to comment.

Accepted Answer

Steve Eddins
Steve Eddins on 8 Mar 2021
One idea is to use a timetable and the retime function. Suppose I have the following matrix for a 4-minute charge and a 3-minute charge. (I won't be using your original third column since that information is duplicated by the difference between elements in the first column.)
>> x = [3849 1; 3853 2];
I'm going to add a final row to simplify the coding just a bit:
>> x = [x ; 3856 0]
x =
3849 1
3853 2
3856 0
Now, make a timetable:
>> T = timetable(minutes(x(:,1)),x(:,2))
T =
3×1 timetable
Time Var1
________ ____
3849 min 1
3853 min 2
3856 min 0
Adjust the second variable based on the charge duration:
>> T.Var1(1:end-1) = T.Var1(1:end-1) ./ minutes(diff(T.Time))
T =
3×1 timetable
Time Var1
________ _______
3849 min 0.25
3853 min 0.66667
3856 min 0
Finally, use retime:
>> T2 = retime(T,'minutely','previous')
T2 =
8×1 timetable
Time Var1
________ _______
3849 min 0.25
3850 min 0.25
3851 min 0.25
3852 min 0.25
3853 min 0.66667
3854 min 0.66667
3855 min 0.66667
3856 min 0

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!