3 views (last 30 days)

Show older comments

Hello,

I have a big file with multiple rows and columns. I need to get the average of every 30 rows. for example from row 1 to 30, 31 to 60, 61 to 90 and so on until the end of the rows. Can anyone help me with this? any kinds of help will be really appriciated.

Shlomo Geva
on 5 Mar 2021

Just use

for i=1:30:floor(size(M)/30)*30

% loop body will execute and skip 30 rows, until there is no more group of exactly 30 rows left.

...

end

Walter Roberson
on 5 Mar 2021

M = your matrix with 10039 rows and 64 columns

N = 30;

whole_blocks = floor(size(M,1)/N);

leftover = size(M,1) - whole_blocks * N;

last_whole = whole_blocks * N;

whole_mean = reshape( mean(reshape(M(1:last_whole, N, [])), 2), whole_blocks, [] );

extra_mean = mean(M(last_whole+1:end, :),1);

overall_mean = [whole_mean; extra_mean];

KALYAN ACHARJYA
on 4 Mar 2021

Edited: KALYAN ACHARJYA
on 4 Mar 2021

As you mentioned, the size of the data is "10039 columns, 64 rows"

Load the file using readmatrix or load function.

Easiest Way:

cell_data=mat2cell(data,[30,30,4]);

%.........................^ Row data

avg_data=zeros(1,size(cell_data,1));

for i=1:size(cell_data,1)

avg_data(i)=mean2(cell_data{i});

end

avg_data

Note:

1. If you have the different rows number & divisible by 30 or step size, you can create a 1D row array easily

row_data=30*ones(1,rows/30);

An example rows size is 300, in that case it would be

row_data =

30 30 30 30 30 30 30 30 30 30

2. As the given rows number is not divisible by 30 exactly (64 case), hence I have created rows vector [30 30 4], you can make it generalize

3. If the all cell elements are equal size in cell data, such case you can easiliy avoid the for loop here by data sequzeeing in cat 3 dimention, afterwards apply the mean on individual planes (3rd Dimension).

Hope it Helps!

Shlomo Geva
on 4 Mar 2021

Edited: Walter Roberson
on 5 Mar 2021

%% load the fille into memory with

M = csvread(filename)

%% loop around M, 30 rows at a time

for i=1:30:size(M,1)

m = mean(M(i:i+29,:)); % compute mean of rows from row i to row i+29

% here do something with the mean

end

Shlomo Geva
on 5 Mar 2021

Here is a solution to also take care of a number of rows that is not a multiple of your grouping (e.g. 30).

I made up a matrix of random integers (between 1 and 10), having 10 rows and 2 columns.

The code computes the mean of the rows, in groups of 3 rows at a time.

M = randi(10,10,2); % your matrix should go here - read it from csv

disp(M); % show the matrix

N = 3; % your number of vectors in a group that you wish to average (in your case 30)

S = size(M,1); % number of rows in M

for i=1:N:S

if i<=S-N

% here if we have N rows - safe to average

m = mean(M(i:i+N-1,:));

elseif S==i

% here if only one row left - no need to average

m = M(i,:);

else

% here taking the average of left over rows (more than one).

m = mean(M(i:end,:));

end

disp(m); % replace this by whatever you wish to do with each row

end

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!