Mean of matrix by columns
古いコメントを表示
Hello! I'm looking for a function to get the mean of two (or more) columns:
A = 1 2 3
4 5 6
7 8 9
f(A) =
1.5000 2.5000
4.5000 5.5000
7.5000 8.5000
How can I do that in a simple way, for a generic matrix? Thank you.
採用された回答
その他の回答 (2 件)
Dear Alessandro, you can use MATLAB function "mean" for this purpose. Here is description: http://www.mathworks.com/help/matlab/ref/mean.html. In Your case just use
mean(A)
6 件のコメント
Alessandro Masullo
2013 年 10 月 13 日
sixwwwwww
2013 年 10 月 13 日
You need mean of two consecutive elements in a row? For example,
A = [1 2 3 4]
You need
meanA = [1.5 2.5 3.5]
Is it what you need?
Alessandro Masullo
2013 年 10 月 13 日
Here is an example code for this:
A = rand(3);
[m, n] = size(A);
for i = 1:m
for j = 1:n - 1
A_avg(i, j) = (A(i, j) + A(i, j + 1))/2;
end
end
disp(A)
disp(A_avg)
Is it ok?
Alessandro Masullo
2013 年 10 月 13 日
sixwwwwww
2013 年 10 月 13 日
I think in anyway you will need to use at least one for loop. However if I will have better idea then I will come back to it
Jan
2013 年 10 月 13 日
B = (A(1:end-1) + A(2:end)) * 0.5
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!