find max for every 7 rows

1 visualizzazione (ultimi 30 giorni)
akk
akk il 12 Gen 2024
Commentato: madhan ravi il 12 Gen 2024
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

Risposte (2)

Hassaan
Hassaan il 12 Gen 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);
27.5830 27.4670
disp(maxTempTimes);
13-Nov-2023 01:34:55 13-Nov-2023 01:44:45
% 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
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Star Strider
Star Strider il 12 Gen 2024
Use either the reshape or the Signal Processing Toolbox buffer function for each variable.
Example —
Data = randn(49,2)
Data = 49×2
0.5978 0.5436 0.1103 1.0647 0.8562 0.6926 1.0365 0.1963 1.7994 -0.0170 -0.5542 -1.4675 -0.3199 0.6396 1.2214 0.3263 0.0684 -1.1750 -0.7619 0.8779
Data1 = reshape(Data(:,1), 7, [])
Data1 = 7×7
0.5978 1.2214 -0.3086 -0.9967 -0.3471 1.0621 -1.6629 0.1103 0.0684 0.3389 -0.3594 -1.5991 -1.0413 -1.3425 0.8562 -0.7619 0.5609 -0.4215 0.0814 1.0672 0.5444 1.0365 -0.8453 0.2523 -0.7036 -1.1593 1.4468 1.7031 1.7994 -0.5757 0.4825 0.8242 -0.3033 -0.9081 1.7472 -0.5542 -0.7064 -0.0526 -0.0912 0.0708 -1.3495 0.0473 -0.3199 0.3621 0.1043 1.7174 -0.5343 1.0415 1.1559
Mean_Data1 = max(Data1)
Mean_Data1 = 1×7
1.7994 1.2214 0.5609 1.7174 0.0814 1.4468 1.7472
Data2 = reshape(Data(:,2), 7, [])
Data2 = 7×7
0.5436 0.3263 0.2197 -1.1096 -1.5440 0.3718 -0.1708 1.0647 -1.1750 -0.3945 -1.2218 0.8989 -1.4921 0.1215 0.6926 0.8779 0.2005 -0.7502 0.9958 2.0077 -0.8095 0.1963 0.5555 0.5576 -0.5489 0.0625 -0.2615 -1.1513 -0.0170 0.5829 0.8592 -0.0001 -0.2124 -0.0694 -0.2001 -1.4675 0.7247 0.0086 -0.7447 -1.2748 0.3801 -1.8161 0.6396 0.0286 0.8478 0.8930 -0.4444 0.7717 0.1539
Mean_Data2 = max(Data2)
Mean_Data2 = 1×7
1.0647 0.8779 0.8592 0.8930 0.9958 2.0077 0.1539
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 Commenti
Star Strider
Star Strider il 12 Gen 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.
madhan ravi
madhan ravi il 12 Gen 2024
Understandable ;)

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by