calculate the mean of several columns

11 ビュー (過去 30 日間)
Sobhan
Sobhan 2012 年 4 月 4 日
コメント済み: Walter Roberson 2015 年 12 月 2 日
Hi all, I am new to Matlab and I am struggling with this problem. I have a matrix with 112 columns and 100 rows. How can I calculate the mean of column 1-7 , 8-14, 15-21,...., 106-112 and put the a new matrix with 16 columns and 100 rows. I appreciate your help Sobhan
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2012 年 4 月 5 日
eg
A = rand(100,112);
fun = @(block_struct)mean(block_struct.data,2);
out = blockproc(A,[100,7],fun);
Walter Roberson
Walter Roberson 2015 年 12 月 2 日
KAUST comments "The best answer"

サインインしてコメントする。

採用された回答

Wayne King
Wayne King 2012 年 4 月 4 日
One way is simply with a for loop
A = randn(100,112);
for nn = 0:15
B(:,nn+1) = mean(A(:,nn*7+1:(nn+1)*7),2);
end
  1 件のコメント
Oleg Komarov
Oleg Komarov 2012 年 4 月 4 日
I bet this is more efficient than the one liner and definitely more readable.

サインインしてコメントする。

その他の回答 (3 件)

Oleg Komarov
Oleg Komarov 2012 年 4 月 4 日
A = rand(100,112);
B = reshape(mean(reshape(A.',7,[])),16,100).';
% Check
isequal(mean(A(2,8:14)),B(2,2))

Thomas
Thomas 2012 年 4 月 4 日
You could reshape your matrix with 7 columns becoming 1 in the new matrix and taking the mean of each column
Eg.:
c=rand(4) % 4x4 matrix
d=reshape(c,[],2) % take two colm and form 1 total 2
p=mean(d) % mean of each columns
In your case
c=rand(100,112) % 4x4 matrix
d=mean(reshape(c,7,[])) % form 1600 columsns
final_out=(reshape(d,16,100)) % mean of each columns
  3 件のコメント
Thomas
Thomas 2012 年 4 月 4 日
thanks, edited accordingly..
Oleg Komarov
Oleg Komarov 2012 年 4 月 4 日
Now, your mean is across the first 7 rows (first column). You have to transpose and re-transpose back. Honestly, in this cases I think Waynes solution is best in terms or readability and performance.

サインインしてコメントする。


Sobhan
Sobhan 2012 年 4 月 5 日
Dear all, Waynes` solution worked like a charm. You are awesome. Thanks a lot for your nice suggestions :)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by