Creating a weighted function array/vector

10 views (last 30 days)
Hi,
I am trying to compute a time-dependent operation. for example no. of people taking the train in a day. I have a row vector with values that represent no. of. people taking the train each second of the day. The problem is the end result is a not very realistic because it doesn't factor in the working hours & peak transport hours.
So, I am trying to create a vector that would serve as a weighted function that assigns weights to certain each second of the day, say peak/non-peak hours.
I couldn't figure out a way to create this vector. Is someone aware of a way to generate such a vector or is there a built-in function to generate one?
Thanks in advance!
  1 Comment
Dyuman Joshi
Dyuman Joshi on 8 Feb 2024
How would that vector be generated? Are the values random? Or is there a logic followed to obtain them?

Sign in to comment.

Accepted Answer

Hassaan
Hassaan on 8 Feb 2024
  1. Initialize the Vector: Create a vector of length 24×60×6024×60×60 for each second in a day, initially filled with the non-peak weight.
  2. Define Peak Hours: Determine the ranges for peak hours. For example, morning peak hours might be from 7 AM to 9 AM, and evening peak hours from 5 PM to 7 PM.
  3. Assign Weights: Assign a higher weight to the peak hours. In MATLAB, you can directly access and modify specific ranges of the vector using indices.
% Number of seconds in a day
seconds_in_day = 24 * 60 * 60;
% Initialize vector with non-peak weight (e.g., 1)
weights = ones(seconds_in_day, 1);
% Define peak hours (in seconds from the start of the day)
morning_peak_start = 7 * 60 * 60; % 7 AM
morning_peak_end = 9 * 60 * 60; % 9 AM
evening_peak_start = 17 * 60 * 60; % 5 PM
evening_peak_end = 19 * 60 * 60; % 7 PM
% Assign a higher weight to peak hours, e.g., 2
weights(morning_peak_start:morning_peak_end) = 2;
weights(evening_peak_start:evening_peak_end) = 2;
% Optionally, adjust for mid-day or off-peak variations if needed
mid_day_start = 12 * 60 * 60;
mid_day_end = 14 * 60 * 60;
weights(mid_day_start:mid_day_end) = 1.5;
% Convert seconds to hours for plotting
time_hours = (1:seconds_in_day) / (60*60);
% Plot the weights over the day
figure;
plot(time_hours, weights);
xlabel('Time (hours)');
ylabel('Weight');
title('Weighted Function for Train Ridership Over a Day');
xticks(0:2:24);
grid on;
Initializes a vector weights that represents the weight for each second in a day, assigns a default weight of 1 for non-peak hours, and then assigns a weight of 2 for designated peak hours. You can adjust the times and weights as needed based on your specific requirements or data.
This weighted vector can now be used to modulate your original data vector (representing the number of people taking the train each second) to more accurately reflect daily ridership patterns, including the impact of peak and non-peak hours.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!