How do I read one column A up to a certain value and then take the average of all values of column B up to that certain value in A

1 view (last 30 days)
I want to read the first column of the table (Times) and when time is <=100, i want to take the average of all the values of column 2(PeakAmpdB) and store that
then continue reading between >100 and <= 200 and similarly take average of all the values in couln 2 that meet that criteria. I want to do that till time <=2500.
Repeat the process for columns 3 and 4 then 5 and 6 and so on. In the end plot all these averages agains time(100-200, 200-300, ...2400-2500). Could you guys please help me?
Thank you

Accepted Answer

Cris LaPierre
Cris LaPierre on 7 Jan 2020
Edited: Cris LaPierre on 7 Jan 2020
This sounds like something you could use groupsummary for. The challenge is to create the grouping variable. It'd be easier to play around with if we had your actual data, but I might try something like this.
% Create dummy data to test
Times = [14.61:0.1:2500]';
PeakAmpdB = linspace(85.83,1000,length(Times))';
data = table(Times,PeakAmpdB)
% Create groups for everly 100
data.Grps = ceil(Times/100);
% Compute mean of PeakAmpdB for every 100 time step
meanTbl = groupsummary(data,'Grps',"mean","PeakAmpdB")
  6 Comments
Cris LaPierre
Cris LaPierre on 7 Jan 2020
If it makes more sense, you could also use findgroups and splitapply. Note that this approach assumes each group has groups 1 - 25.
% now with findgroups and splitapply
G0 = findgroups(ceil(H1matlab_Amp.Times/100));
G1 = findgroups(ceil(H1matlab_Amp.Times1/100));
G2 = findgroups(ceil(H1matlab_Amp.Times2/100));
G3 = findgroups(ceil(H1matlab_Amp.Times3/100));
G4 = findgroups(ceil(H1matlab_Amp.Times4/100));
% Compute the mean PeakAmpdB# value for each group
PeakAmpdB = splitapply(@mean,H1matlab_Amp.PeakAmpdB,G0);
PeakAmpdB1 = splitapply(@mean,H1matlab_Amp.PeakAmpdB1,G1);
PeakAmpdB2 = splitapply(@mean,H1matlab_Amp.PeakAmpdB2,G2);
PeakAmpdB3 = splitapply(@mean,H1matlab_Amp.PeakAmpdB3,G3);
PeakAmpdB4 = splitapply(@mean,H1matlab_Amp.PeakAmpdB4,G4);
T = [1:25]'*100;
% Plot results for first 25 groups (100 to 2500 seconds)
plot(T,PeakAmpdB(1:25),...
T,PeakAmpdB1(1:25),...
T,PeakAmpdB2(1:25),...
T,PeakAmpdB3(1:25),...
T,PeakAmpdB4(1:25))
Usama Javid
Usama Javid on 8 Jan 2020
Thank you Cris for all your help. The codes (both of them) are working perfectly now. You have been a great help! I did not know about the grouping and splitapply functions. This has helped me to understand how to use and apply them.
Many Thanks

Sign in to comment.

More Answers (0)

Categories

Find more on Preprocessing Data 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!