Assign values in a matrix
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jorg Woehl
2021-3-8
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?
采纳的回答
Steve Eddins
2021-3-8
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
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!