How to calculate average for data every nth row?

4 views (last 30 days)
I have a matrix of size 900 x 9. I want to average (and find standard errors) for the data located after 30 rows. To be very clear, the first row of the resulting matrix should have the average of (1st row, 31st row, 61st row,...); then the 2nd row of the resulting matrix should have (2nd row, 302d row, 62nd row,...) and so on. Please help.
  1 Comment
Arup Bhattacharya
Arup Bhattacharya on 28 May 2020
This is my problem:
I have a matrix, let's say A(900,9). It stores measurement of 30 seconds worth of data for 30 repeatitions. So, row 1 of matrix A is comparable to row 31, row 2 is comparable to row 32, etc.
Now, I need to match up these rows and calculate their average value.
Let's assume that I have vroken doen matrix A in 30 matrices, each of size (30,9). For example,
A1=zeros(30,9,30);
A1(:,:,1)=A(1:30,:);
A1(:,:,2)=A(31:60,:);
A1(:,:,3)=A(61:90,:);
A1(:,:,4)=A(91:120,:);
A1(:,:,5)=A(121:150,:);
A1(:,:,6)=A(151:180,:);
A1(:,:,7)=A(181:210,:);
A1(:,:,8)=A(211:240,:);
A1(:,:,9)=A(241:270,:);
A1(:,:,10)=A(271:300,:);
A1(:,:,11)=A(301:330,:);
A1(:,:,12)=A(331:360,:);
A1(:,:,13)=A(361:390,:);
A1(:,:,14)=A(391:420,:);
A1(:,:,15)=A(421:450,:);
A1(:,:,16)=A(451:480,:);
A1(:,:,17)=A(481:510,:);
A1(:,:,18)=A(511:540,:);
A1(:,:,19)=A(541:570,:);
A1(:,:,20)=A(571:600,:);
A1(:,:,21)=A(601:630,:);
A1(:,:,22)=A(631:660,:);
A1(:,:,23)=A(661:690,:);
A1(:,:,24)=A(691:720,:);
A1(:,:,25)=A(721:750,:);
A1(:,:,26)=A(751:780,:);
A1(:,:,27)=A(781:810,:);
A1(:,:,28)=A(811:840,:);
A1(:,:,29)=A(841:870,:);
A1(:,:,30)=A(871:900,:);
Now, finally I need to create a matrix, lets say B (30,9) that has the average of the rows of A1 matrices in rows.
I need some help write the code that creates matrix B from matrix A.

Sign in to comment.

Answers (1)

Rik
Rik on 26 May 2020
Something like this should do the trick:
data=rand(900,9);
row_interval=30;
avg=zeros(1,row_interval);
stderr=zeros(1,row_interval);
for n=1:row_interval
tmp=data(1:row_interval:end,:);
tmp=tmp(:);
avg(n)=mean(tmp);
stderr(n)=std(tmp);
end
Of course, you could also mean something like this:
data=rand(900,9);
row_interval=30;
avg=zeros(row_interval,size(data,2));
stderr=zeros(row_interval,size(data,2));
for n=1:row_interval
tmp=data(1:row_interval:end,:);
avg(n,:)=mean(tmp,1);
stderr(n,:)=std(tmp,1);
end
  1 Comment
Arup Bhattacharya
Arup Bhattacharya on 28 May 2020
Sorry for my late reply.
This is my problem:
I have a matrix, let's say A(900,9). It stores measurement of 30 seconds worth of data for 30 repeatitions. So, row 1 of matrix A is comparable to row 31, row 2 is comparable to row 32, etc.
Now, I need to match up these rows and calculate their average value.
Let's assume that I have vroken doen matrix A in 30 matrices, each of size (30,9). For example,
A1=zeros(30,9,30);
A1(:,:,1)=A(1:30,:);
A1(:,:,2)=A(31:60,:);
A1(:,:,3)=A(61:90,:);
A1(:,:,4)=A(91:120,:);
A1(:,:,5)=A(121:150,:);
A1(:,:,6)=A(151:180,:);
A1(:,:,7)=A(181:210,:);
A1(:,:,8)=A(211:240,:);
A1(:,:,9)=A(241:270,:);
A1(:,:,10)=A(271:300,:);
A1(:,:,11)=A(301:330,:);
A1(:,:,12)=A(331:360,:);
A1(:,:,13)=A(361:390,:);
A1(:,:,14)=A(391:420,:);
A1(:,:,15)=A(421:450,:);
A1(:,:,16)=A(451:480,:);
A1(:,:,17)=A(481:510,:);
A1(:,:,18)=A(511:540,:);
A1(:,:,19)=A(541:570,:);
A1(:,:,20)=A(571:600,:);
A1(:,:,21)=A(601:630,:);
A1(:,:,22)=A(631:660,:);
A1(:,:,23)=A(661:690,:);
A1(:,:,24)=A(691:720,:);
A1(:,:,25)=A(721:750,:);
A1(:,:,26)=A(751:780,:);
A1(:,:,27)=A(781:810,:);
A1(:,:,28)=A(811:840,:);
A1(:,:,29)=A(841:870,:);
A1(:,:,30)=A(871:900,:);
Now, finally I need to create a matrix, lets say B (30,9) that has the average of the rows of A1 matrices in rows.
I need some help write the code that creates matrix B from matrix A.

Sign in to comment.

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!