Average Calculation and Array in For Loop
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I collected two data sets. Data 1 collected a data point every 0.01 seconds, while Data 2 collected a data point every 1 min. They were taken at the same time, so they will have the same time stamps.
What I wanted to do was to average all the values of Data 1 for every minute, so that I will end up with the same amount of data points as Data2. I was thinking of using a for loop top iterate through every element of data1. Then, check if the minute of element i is the ssme as the minute of the previous element i-1. If so, I added the two numbers into a variable x and increased the count by count = count + 1. This went one until a new minute was reached. Then, the average of all previous elements (corresponding to the "old" minute) would be calculated by x/count. Then, this value would be appended into a new array called averages. Finally, for the new minute, the sum x would be resetted by x = data1(i) and the count would also be resetted to 1.
The code is attached below. However, when I run it it gives me the error: Error using count. Not enough input arguments. Error in plots_data (line 31): average = x / count; What is wrong?
for i = 1:length(data1)
% Check if the minute of the current data point is the same as the previous one
if i > 1 && m(i) == m(i-1) % m are the minutes
% If it is, accumulate the thrust value and increase the count
x = x + data1(i);
count = count + i;
else
% If it's a new minute, calculate the average thrust for the previous minute
average = x / count;
averages = [averages; average];
% Reset sum and count for the new minute
x = data1(i);
count = 1;
end
end
0 commentaires
Réponses (2)
Tony
le 5 Juin 2024
In the first iteration, it's attempting to calculate the average of the "0th" minute, through the first two lines in the else block. I suggest modifying the else block as follows,
else
if i > 1 % If it's a new minute, calculate the average thrust for the previous minute
average = x / count;
averages = [averages; average];
end
% Reset sum and count for the new minute
x = data1(i);
count = 1;
end
0 commentaires
Steven Lord
le 5 Juin 2024
When i is equal to 1, attempting to index into m with i-1 (otherwise known as 0) as an index would error if the short-circuit && operator didn't cause MATLAB not to run that code. [I'm commenting out this line of code that would error when I ran it in MATLAB Answers so I can run later lines in my answer.]
%{
if i > 1 && m(i) == m(i-1) % m are the minutes
%}
Therefore the count variable was not created in that first iteration. When you try to use it in the else section of the statement:
which -all count
MATLAB tries to call the count function with 0 input arguments instead and that won't work.
y = count
Instead, if you have your time stamped data I would create two timetable arrays. This lets you store the time information and the data collected at those times in one variable. Once you have those two timetable arrays you can use synchronize to generate a new timetable with data from both those timetables, where the new timetable has a common set of times.
0 commentaires
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!