How do I write time vector starting from midnight (12:00 am) hourly to 24th hour using time-intervals of 6 minutes (tenths of an hour).
9 次查看(过去 30 天)
显示 更早的评论
I have to declare two variables: time and P(t), how do I do it?
Time is already explained in my question and P(t) has different values for different times for example:
For t<6, P(t)=0
Like how do I start my code I do not undertand
0 个评论
回答(1 个)
Vaibhav
2024-2-9
编辑:Vaibhav
2024-2-9
Hi Maha
You can create a time vector that starts from midnight and increments every 6 minutes until the 24th hour using the "datetime" and "minutes" functions. You can then use logical indexing or a loop to define the variable "P(t)" based on the conditions you've provided.
You can refer the code snippet below:
% Define the start and end times
startTime = datetime('today', 'Format', 'dd-MMM-yyyy HH:mm:ss');
endTime = startTime + days(1);
% Create a time vector with 6-minute intervals
time = startTime:minutes(6):endTime;
% Preallocate P(t) with zeros
P = zeros(size(time));
% Define P(t) based on the condition
for i = 1:length(time)
t = hours(time(i) - startTime); % Convert time to hours since midnight
if t < 6
P(i) = 0; % For t < 6, P(t) = 0
else
% Add other conditions for P(t) here
% For example, if you have other conditions for different time ranges,
% you would add them in this section using elseif or else statements.
end
end
% Note: The time vector includes the 24th hour (midnight of the next day),
% if you want to exclude it, you can use:
% time = startTime:minutes(6):(endTime - minutes(1));
time
You can refer to the MathWorks documentation links below to learn more about "datetime" and minutes functions:
Hope this helps!
3 个评论
Walter Roberson
2024-2-9
No, your resulting t will be in hours, but none of MATLAB's time systems run in hours. The old MATLAB time system runs in days (since January 1, 0000), and the newer MATLAB time system uses objects (that are internally a pair of 64 bit integers) to represent time.
With the newer MATLAB time system, you have your choice between absolute times ( datetime objects) and relative times ( duration objects). @Vaibhav already showed absolute times. For durations it would look like
t = hours(0) : minutes(6) : hours(24);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!