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 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Vaibhav
le 9 Fév 2024
Modifié(e) : Vaibhav
le 9 Fév 2024
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 commentaires
Walter Roberson
le 9 Fév 2024
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);
Voir également
Catégories
En savoir plus sur Dates and Time dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!