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

2 次查看(过去 30 天)
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 个评论
Shlomo Geva
Shlomo Geva 2021-3-5
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
Walter Roberson 2021-3-5
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 个评论
Walter Roberson
Walter Roberson 2021-3-5
@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

请先登录,再进行评论。

更多回答(3 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021-3-4
编辑:KALYAN ACHARJYA 2021-3-4
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 个评论
Minions
Minions 2021-3-4
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

请先登录,再进行评论。


Shlomo Geva
Shlomo Geva 2021-3-4
编辑:Walter Roberson 2021-3-5
%% 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 个评论
Shlomo Geva
Shlomo Geva 2021-3-5
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.)

请先登录,再进行评论。


Shlomo Geva
Shlomo Geva 2021-3-5
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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by