how to get average of multiple rows and subtract from one group of average to another

5 visualizzazioni (ultimi 30 giorni)
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.
  8 Commenti
Shlomo Geva
Shlomo Geva il 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

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 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];
  3 Commenti
Walter Roberson
Walter Roberson il 5 Mar 2021
@Shlomo Geva You are right, my code was buggy.
M = rand(10039,64);
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, [], size(M,2)), 1),[],size(M,2));
extra_mean = mean(M(last_whole+1:end, :),1);
overall_mean = [whole_mean; extra_mean];
size(whole_mean)
ans = 1×2
334 64
size(extra_mean)
ans = 1×2
1 64
size(overall_mean)
ans = 1×2
335 64

Accedi per commentare.

Più risposte (3)

KALYAN ACHARJYA
KALYAN ACHARJYA il 4 Mar 2021
Modificato: KALYAN ACHARJYA il 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!
  1 Commento
Minions
Minions il 4 Mar 2021
thank you for your answer, I tried to run the programming, but i can find error. Also can you please explain how did you do it. I am kinda new in matlab so difficult for me to understand. Thank you in advance

Accedi per commentare.


Shlomo Geva
Shlomo Geva il 4 Mar 2021
Modificato: Walter Roberson il 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
  7 Commenti
Shlomo Geva
Shlomo Geva il 5 Mar 2021
Yes, Walter Roberson, thanks.
That's an udocumented feature :-)
The number of rows has to be a multiple of 30.
If it is not, then the final few rows have to treated separately. Furthermore, if there is only one row left then the row itself is probably the desirable mean (taking the mean of a vector will give a scalar which not desirable one might think.)

Accedi per commentare.


Shlomo Geva
Shlomo Geva il 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

Community Treasure Hunt

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

Start Hunting!

Translated by