- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
find max for every 7 rows
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a timetable matrix (A) that has many variables (e.g., temp, salinity, etc). The instrument was programmed to collect measurements as bursts of 7 within a given time period, but some of these measurements are errors that tend to be lower/higher then they should be. I would like to ask Matlab to move a column (e.g., Temp) and pull out the max value for every group of 7 rows such that 27.583 would come out of the first group of 7 for temperature and 27.467 would come out for the second group of 7. Then I would need the matching time for that row so that I can plot it.
IntD IntT (UTC) Press Temp
YYYY-MM-DD hh:mm:ss.sss dBar °C
2023-11-13 01:34:35.120 1.2 27.563
2023-11-13 01:34:40.090 1.21 27.57
2023-11-13 01:34:45.030 1.21 27.578
2023-11-13 01:34:50.110 1.21 27.581
2023-11-13 01:34:55.060 1.21 27.583
2023-11-13 01:35:00.010 1.23 27.582
2023-11-13 01:35:05.090 1.2 27.582
2023-11-13 01:44:35.120 1.17 27.432
2023-11-13 01:44:40.090 1.2 27.456
2023-11-13 01:44:45.030 1.24 27.467
2023-11-13 01:44:50.110 1.2 27.444
2023-11-13 01:44:55.050 1.2 27.441
2023-11-13 01:45:00.130 1.21 27.439
2023-11-13 01:45:05.070 1.19 27.429
0 Kommentare
Antworten (2)
Hassaan
am 12 Jan. 2024
% Create dummy data for the timetable matrix
IntD = datetime({'2023-11-13 01:34:35.120'; '2023-11-13 01:34:40.090'; '2023-11-13 01:34:45.030'; '2023-11-13 01:34:50.110'; '2023-11-13 01:34:55.060'; '2023-11-13 01:35:00.010'; '2023-11-13 01:35:05.090'; '2023-11-13 01:44:35.120'; '2023-11-13 01:44:40.090'; '2023-11-13 01:44:45.030'; '2023-11-13 01:44:50.110'; '2023-11-13 01:44:55.050'; '2023-11-13 01:45:00.130'; '2023-11-13 01:45:05.070'}, 'InputFormat', 'yyyy-MM-dd HH:mm:ss.SSS');
Press = [1.2; 1.21; 1.21; 1.21; 1.21; 1.23; 1.2; 1.17; 1.2; 1.24; 1.2; 1.2; 1.21; 1.19];
Temp = [27.563; 27.57; 27.578; 27.581; 27.583; 27.582; 27.582; 27.432; 27.456; 27.467; 27.444; 27.441; 27.439; 27.429];
% Create the timetable matrix
A = timetable(IntD, Press, Temp);
% Initialize variables to store the maximum values and corresponding times
maxTemps = [];
maxTempTimes = [];
% Define the number of rows in each group (7 in your case)
groupSize = 7;
% Iterate through the rows of the timetable in steps of groupSize
for i = 1:groupSize:numel(A.Temp)
% Extract the group of rows
group = A(i:i+groupSize-1, :);
% Find the maximum temperature value in the group
[maxTemp, idx] = max(group.Temp);
% Find the corresponding time for the maximum temperature
maxTime = group.IntD(idx);
% Append the maximum temperature and time to the respective arrays
maxTemps = [maxTemps; maxTemp];
maxTempTimes = [maxTempTimes; maxTime];
end
% Display the maximum temperatures and their corresponding times
disp(maxTemps);
disp(maxTempTimes);
% Plot the data
plot(maxTempTimes, maxTemps);
xlabel('Time');
ylabel('Max Temperature (°C)');
title('Maximum Temperature Over Time');
This code uses dummy data to create the timetable matrix A and then applies the same logic as before to find the maximum temperature values and their corresponding times in groups of 7 rows, and finally, it plots the maximum temperatures over time.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
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.
Professional Interests
Feel free to contact me.
0 Kommentare
Star Strider
am 12 Jan. 2024
Example —
Data = randn(49,2)
Data1 = reshape(Data(:,1), 7, [])
Mean_Data1 = max(Data1)
Data2 = reshape(Data(:,2), 7, [])
Mean_Data2 = max(Data2)
The buffer function does not care if the row size is not an integer multiple of 7. It fills the last column with zeros if necessary to fill out the matrix.
.
3 Kommentare
Star Strider
am 12 Jan. 2024
If the table had been provided as a file, I would have written code specifically using it. Reconstructring it from the posted text is a bit more than I am up for this morning.
Siehe auch
Kategorien
Mehr zu Time Series finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!