I have a 100x100 matrix, i need average of 10-10 row values, so that my matrix become 10x100

6 次查看(过去 30 天)
I have a 100x100 matrix, i need average of 10-10 row values, so that my matrix become 10x100

回答(2 个)

KSSV
KSSV 2017-11-9
A = rand(100) ;
m = 10 ; n = 100 ;
l = size (A) ./ [m n];
T = mat2cell (A, repmat (m, l(1), 1), repmat (n, l (2), 1));
M = cellfun(@mean,T,'un',0) ;
M = cell2mat(M) ;

Stephen23
Stephen23 2017-11-9
编辑:Stephen23 2017-11-9
This is actually really simple with reshape. Here is an example with a 6x4 matrix where I average every 3 rows to give a 2x4 matrix:
>> M = randi(9,6,4)
M =
8 6 8 4
3 7 9 8
5 2 5 6
8 2 8 6
2 3 2 3
4 3 4 7
>> N = 3; % number of rows to average over
>> R = size(M,1)/N % final number of rows: must be integer!
R = 2
>> reshape(mean(reshape(M,N,[]),1),R,[])
ans =
5.3333 5.0000 7.3333 6.0000
4.6667 2.6667 4.6667 5.3333
And checking the first value by hand:
>> mean([8,3,5])
ans = 5.3333

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by