Make function reset for every interval/day
7 views (last 30 days)
Show older comments
Hello,
I'm trying to make a loop where I want my function to reset it's calculation for every day. My array is sectioned into minutes.
So far I've made an array containing all index for every values my function needs to do it's calculation for e.g.:
di = [1 1 1 2 2 2 3 3 3 ] which corresponds to the three first days for three minutes. So I have every number repeating 60*24 times. I've used the unique function for creating this.
I have another array called t which is simply just values from a previous calculation.
First I want a function x = t*2 and I want it to reset this for each day, so every time a value in di changes.
Then I want it to cummulate all values in x for each time di changes it's value (1,2,3 ect.).
I know some have asked for similar questions (sorry) , but I cannot get their solutions to work.
I'm using Matlab2019a.
0 Comments
Answers (1)
Deepak
on 14 Nov 2024 at 10:21
As I understand, the task is to perform and accumulate daily calculations on minute-by-minute time-series data without overwriting previous results.
To achieve this, first use the “unique” function to identify all the unique days in “di”. Then, loop through each unique day and use logical indexing to extract the corresponding values from the t array. For each day, calculate the desired function, such as multiplying the values by 2, and then compute the cumulative sum of these results.
Here is the sample MATLAB code to achieve the same:
di = [1 1 1 2 2 2 3 3 3];
t = rand(size(di)); % Example array 't' with random values
% Find unique days
unique_days = unique(di);
% Initialize array to store cumulative results for each day
cumulative_results = zeros(size(unique_days));
for i = 1:length(unique_days)
day = unique_days(i);
% Find indices corresponding to the current day
indices = (di == day);
x = t(indices) * 2;
cumulative_results(i) = sum(x);
end
disp('Cumulative results for each day:');
disp(cumulative_results);
For more information on the functions used refer to the following documentation:
I hope this helps in resolving the issue.
0 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!