필터 지우기
필터 지우기

Assign values in a matrix

조회 수: 2 (최근 30일)
Joel Schelander
Joel Schelander 2021년 3월 8일
답변: Steve Eddins 2021년 3월 8일
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
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?
Joel Schelander
Joel Schelander 2021년 3월 8일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Steve Eddins
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개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by