Assign values in a matrix

1 visualizzazione (ultimi 30 giorni)
Joel Schelander
Joel Schelander il 8 Mar 2021
Risposto: Steve Eddins il 8 Mar 2021
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 Commenti
Jorg Woehl
Jorg Woehl il 8 Mar 2021
Hi Joel, I am not quite clear on what you would like to do... Do you want to change all values in the second column of your n-by-3 matrix to 0.25? Or is this a conditional assignment, like in "if the second column is 1, then change it to 0.25"? And reset the third column to 1 at the same time? Or do you want to "add values", like you are saying, i.e. add new rows to your matrix where the value in the first column (charging start time) increases by 1 from one row to the next?
Joel Schelander
Joel Schelander il 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

Accedi per commentare.

Risposta accettata

Steve Eddins
Steve Eddins il 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

Più risposte (0)

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by