moving averege of a matrix

3 ビュー (過去 30 日間)
Rica
Rica 2013 年 3 月 6 日
Hi!
a have a matrix:
%
A=[2 5 6 8 7 ;2 2 3 5 6; 1 2 3 4 2]
how could i calculate the moving averege (order 2) of the matrix horizentally, i mea the vectors a1= [2 5 6 8 7] and a2=[2 2 3 5 6] a3=[1 2 3 4 2] are indepandent .
the moving averege of each vector?
thank you

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 3 月 6 日
out = conv2(A,[.5 .5],'same');
out(:,1:end-1);
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2013 年 3 月 6 日
out = [A(:,1),conv2(A,[.5 .5],'valid')];
Daniel Shub
Daniel Shub 2013 年 3 月 6 日
For an A of 1e3 x 1e5, it appears that Jan's direct method is faster, but if you are not worried about the edge, then your method is faster.

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

その他の回答 (2 件)

Jan
Jan 2013 年 3 月 6 日
編集済み: Jan 2013 年 3 月 7 日
A = [2 5 6 8 7 ;2 2 3 5 6; 1 2 3 4 2];
B = (A(:, 1:4) + A(:, 2:5)) * 0.5;
This is one column shorter than the input, but how is the moving average defined at the edges?
[EDITED] A hard coded moving average for 5 neighboring elements along the rows:
[d1, d2] = size(X);
Z1 = zeros(d1, 1);
Z2 = zeros(d1, 2);
M = X + [Z1, X(:, 1:d1 - 2) + X(:, 3:d1), Z1] + ...
[Z2, X(:, 1:d1 - 4) + X(:, 5:d1), Z2];
divV(1, [1, 2, d1 - 1, d1]) = [1, 3, 3, 1];
divV(1, 3:d1 - 2) = 5;
Y = bsxfun(@rdivide, M, div);
Here the first element is not changed and the 2nd is the average of the elements 1 to 3. I do not assume that this can beat CONV.
  3 件のコメント
Jan
Jan 2013 年 3 月 6 日
Too big for what? Why is the first value unchanged and what exactly is "the first value"?
Rica
Rica 2013 年 3 月 7 日
my matrix is of the size 27*301528. i want the moving averege of each raw. the size of zhe window should be 5. that means or the first 5 value , i build the mean value with the mean function. from the sixth value should the moving averege start.
i am tryin now to use filter2, but i will be happy for other advices.
thank you

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


Image Analyst
Image Analyst 2013 年 3 月 6 日
Moving how? In an m by n window? Or do you simply want the average of the entire row, in which case you can do this:
rowAverages = mean(A, 2);
  1 件のコメント
Image Analyst
Image Analyst 2013 年 3 月 7 日
Rica, if you want the mean of 5 horizontal numbers in a 27 by 302,528 matrix, you can do this:
means = conv2(yourArray, [1 1 1 1 1]/5, 'valid');

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

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by