use of for loop for time shift

2 visualizaciones (últimos 30 días)
aa
aa el 14 de Ag. de 2020
Respondida: Arjun el 5 de Sept. de 2024
I have a data set in two coloums, time and number of events. for every time we have to pick teh crossponding value from second coloumn. I did this by for loop at initial stage. Now I want to pick the number of events in two hours with a shift of one hours. For example, 1-2, 2-3,3-4,4-5 hour.
May someone help me here.
Thank you
  2 comentarios
Sara Boznik
Sara Boznik el 14 de Ag. de 2020
Maybe will be good if you send us the data.
If I correctly understand you, you need only the pick number like 1 2 3 and not 0.5, 1.5 ...
Some idea would be that you use
if rem(num,1)==0
For more I need your task.
Wish you best of luck.
Rafael Hernandez-Walls
Rafael Hernandez-Walls el 14 de Ag. de 2020
If you have this data,
n=10;
x=[1:n]';y=[1:n]';
% shift of one hours
[x(1:n-1) y(2:n)]

Iniciar sesión para comentar.

Respuestas (1)

Arjun
Arjun el 5 de Sept. de 2024
Hi @aa,
As per my understanding, you have a dataset of time vs number of events, and you want to sum up number of events for each two-hour duration.
To achieve this task in MATLAB, you can use a sliding window approach to sum the number of events within each two-hour window, shifting by one hour each time.
We loop through the dataset using a window size of 2, which corresponds to our requirement of considering time frames 1-2, 2-3, 3-4, etc. We have a step size of 1, which means that after scanning the time frame 1-2, the next time frame will be 2-3, and then 3-4. While considering the window 1-2, we sum up all the entries in the number of events column corresponding to the time within the window. We repeat this iteratively for all the time frames.
Kindly refer to the code below to better understand the approach:
% Sample dataset with two columns: [time, number of events]
data = [
1, 5;
2, 3;
3, 6;
4, 2;
5, 8;
6, 7;
7, 4;
8, 5;
9, 9;
10, 1
];
% Initialize an array to store the sum of events in each two-hour window
eventSums = [];
% Define the window size and step size
windowSize = 2;
stepSize = 1;
% Loop through the data array with the specified window and step size
for i = 1:stepSize:(size(data, 1) - windowSize + 1)
% Calculate the sum of events within the current window
windowSum = sum(data(i:i + windowSize - 1, 2));
% Store the result
eventSums = [eventSums, windowSum];
end
% Display the results
disp('Sum of events in each two-hour window:');
disp(eventSums);
I hope it will help!

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by