MATLAB Answers

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


Sign in to comment.

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")


Show 3 older comments
Cris LaPierre
Cris LaPierre on 7 Jan 2020
Got it. The code I shared should do that, but requires a little tweaking to work with your data. Try this.
load my_tabletry.mat
% Identify groups of data (<=100 == 1, >100 & <=200 == 2, etc)
H1matlab_Amp.Grps = ceil(H1matlab_Amp.Times/100);
H1matlab_Amp.Grps1 = ceil(H1matlab_Amp.Times1/100);
H1matlab_Amp.Grps2 = ceil(H1matlab_Amp.Times2/100);
H1matlab_Amp.Grps3 = ceil(H1matlab_Amp.Times3/100);
H1matlab_Amp.Grps4 = ceil(H1matlab_Amp.Times4/100);
% Compute the mean PeakAmpdB# value for each group
meanTbl = groupsummary(H1matlab_Amp,'Grps',"mean","PeakAmpdB");
meanTbl1 = groupsummary(H1matlab_Amp,'Grps1',"mean","PeakAmpdB1");
meanTbl2 = groupsummary(H1matlab_Amp,'Grps2',"mean","PeakAmpdB2");
meanTbl3 = groupsummary(H1matlab_Amp,'Grps3',"mean","PeakAmpdB3");
meanTbl4 = groupsummary(H1matlab_Amp,'Grps4',"mean","PeakAmpdB4");
% Plot the resutls up to 2500 seconds (Grp == 25)
hold on
hold off
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)
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)

Sign in to answer this question.