MATLAB Answers

0

Averaging the previous 5 values of every 30 values in a matrix

Asked by Yoanna Ivanova on 2 Sep 2019
Latest activity Commented on by Yoanna Ivanova on 2 Sep 2019
Hey guys,
I have this matrix that is 521x5 (in column 1 is subject number, in column 2 is experimental time, in column 3 is HR, etc..), I want to select one column from it (HR) and then select every 30th row and find the average of the previous 5 numbers in that column and insert them in a new matrix --- this is a bit confusing. Here is a bit of my code and I hope that makes it a bit more clear:
%read file
numData = xlsread (filename,'some phys parameters');
%experimental time is in column 2
experimentalTime = [30; 60; 90; 120; 150; 180; 210; 240; 270; 300; 330; 360; 390; 420; 450; 480; 510];
%HR is in column 3
HR = numData(31:30:end,3);
--> the code so far selects the HR at every 30 values starting at the 31st value and copies that into a new matrix but I need it to not select every 30th value but to take the average of the previous 5 rows in that column average that and then insert it into the new matrix. How can I do that? The 521 is experimental time in minutes, but I don't need all of that, I only need the experimental time at time points 30, 60, 90, etc and I wanted to average the HR at those time points by averaging the previous 5 entries , hope somehow this makes sense to you

  1 Comment

Please attach your data here as mat - file.

Sign in to comment.

Tags

2 Answers

Answer by darova
on 2 Sep 2019
 Accepted Answer

Use for loop
HR = numData(31:30:end,3);
meanHR = HR*0;
for i = 1:length(HR)
i1 = (-5:-1)+i*30; % (25:30) , (55:60) , (85:90)
meanHR(i) = mean( numData(i1,3) );
end

  1 Comment

This is it. perfect. Thank you so much!!

Sign in to comment.


Answer by Andrei Bobrov
on 2 Sep 2019
Edited by Andrei Bobrov
on 2 Sep 2019

m = size(numData,1);
lo = mod((0:m-1)',30) + 1 >= 26;
ii = ceil((1:m)'/10);
HRmean5 = accumarray(ii(lo),numData(lo,3),[],@mean);

  0 Comments

Sign in to comment.