Create an array of empty durations

조회 수: 26 (최근 30일)
Christian Breinholt
Christian Breinholt 2017년 11월 21일
댓글: Karol Ondrejkovic 2022년 6월 17일
Is there a way to create an array of durations similar to the way I can create an array of strings, zeroes etc. i.e.
str = strings(size, 0)
i tried something like
dur = durations(size, 0)
but that does not work.

채택된 답변

Walter Roberson
Walter Roberson 2017년 11월 21일
편집: Walter Roberson 2017년 11월 21일
duration(nan(NumberOfEntries,3))
If you want a 2D array of durations then you would probably need to reshape() the above.
Alternately you could use
NaT(numrows, numcols) - NaT(1)
to get a numrows x numcols duration array.
  댓글 수: 3
Raphaël Nussbaumer
Raphaël Nussbaumer 2021년 4월 21일
Look like the second method is not working anymore with R2020b at least...
For instance
tmp = NaT(1,2)
tmp(1)=duration(0,0,1)
returns:
Error using datetime/parenAssign (line 66)
Right hand side of an assignment must be a datetime array or text representing dates and times.
Stephen23
Stephen23 2021년 4월 21일
편집: Stephen23 2021년 4월 21일
tmp is a datetime array. The output of duration is a duration array.
What do you expect to occur when you allocate a duration to a datetime array? What date is "five minutes" ?
"Look like the second method is not working anymore with R2020b at least... "
It works when I try it:
X = NaT(2,3)-NaT(1)
X = 2×3 duration array
NaN NaN NaN NaN NaN NaN

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

추가 답변 (2개)

Jan
Jan 2017년 11월 21일
I asssume that https://www.mathworks.com/help/matlab/ref/nat.html: "Not-a-Time" does not help directly. What about:
duration(NaN(size));
  댓글 수: 1
Karol Ondrejkovic
Karol Ondrejkovic 2022년 6월 17일
For me duration(strings(size)) works well.
Example:
duration(strings([2,3]))

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


Steven Lord
Steven Lord 2021년 4월 21일
Another way to create an empty array of duration objects is to use the empty function for the class.
d = duration.empty(5, 0)
d = 5×0 empty duration array
  댓글 수: 2
Raphaël Nussbaumer
Raphaël Nussbaumer 2021년 4월 21일
Nice one! Much better. It would be nice to be able to do:
duration.empty(size(X))
with X another matrix od any size.
Thanks anyway!
Steven Lord
Steven Lord 2021년 4월 21일
As long as X is empty you can.
X = zeros(1, 2, 3, 4, 0);
D = duration.empty(size(X));
size(D)
ans = 1×5
1 2 3 4 0
An empty array in MATLAB must have the size in at least one dimension equal to 0 (as per the documentation for the isempty function) so something like this wouldn't work as the error message clearly states.
X = 1:10;
D = duration.empty(size(X))
Error using duration.empty (line 1095)
At least one dimension must be zero.

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

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by