Averaging values in column 2 if column 1 falls within a certain range
1 view (last 30 days)
Show older comments
Hi,
I am pretty stuck and would really appreciate some/any help or a push in the right direction.
I have matrix "B" which has time(sec) in column 1 and Diameter in column 2.
I am trying to do something like this:
If column 1 is between n and n+0.99 seconds, average all the value in column 2 and also indicate what value 'n' was in the column next to it. Repeat this for n+1 all the way to the end.
so the intervals will be 1 - 1.99 , 2 - 2.999 , 3 - 3.99 all the way to 60 seconds.
Thank you,
Nev
0 Comments
Accepted Answer
Alex Mcaulley
on 29 May 2019
Try this (just changing by your real B):
n = 1:60;
B = rand(1000,2);
B(:,1) = linspace(1,60,1000);
means = arrayfun(@(i) mean(B(B(:,1)>=n(i)&B(:,1)<n(i+1),2)),1:(numel(n)-1));
Nextn = B(arrayfun(@(i) find(B(:,1)>=n(i)&B(:,1)<n(i+1),1,'last'),1:(numel(n)-1))+1,1);
6 Comments
Alex Mcaulley
on 30 May 2019
Yes, this line:
B(:,1) = linspace(1,60,7841);
was just for my example data. The correct code is
n = 1:60;
means = arrayfun(@(i) mean(B(B(:,1)>=n(i)&B(:,1)<n(i+1),2)),1:(numel(n)-1));
Nextn = B(arrayfun(@(i) find(B(:,1)>=n(i)&B(:,1)<n(i+1),1,'last'),1:(numel(n)-1))+1,1);
More Answers (1)
Steven Lord
on 30 May 2019
I would use groupsummary specifying the groupbins input as either a list of bin edges or (if you can convert your time data to a duration array) 'second' and the method as 'mean'.
See Also
Categories
Find more on Creating and Concatenating Matrices 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!