Average Calculation and Array in For Loop
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
endI 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 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 array MATLAB Answers — New Questions