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.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 10월 13일

1 개 추천

conv2(A,[.5 .5],'valid')

추가 답변 (2개)

sixwwwwww
sixwwwwww 2013년 10월 13일
편집: sixwwwwww 2013년 10월 13일

0 개 추천

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

I don't think this is the function I need... mean(A) will do the mean of each column, but what I want is the mean of all the column 2 by 2. I do this in this way, but it is not so good:
for k = 1:3
avgA(:,k) = (A(:,k) + A(:,k+1))/2;
end
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?
Yes, but the same thing for all the column of a matrix:
A = 1 <-> 2 <-> 3
4 <-> 5 <-> 6
7 <-> 8 <-> 9
f(A) =
1.5000 2.5000
4.5000 5.5000
7.5000 8.5000
sixwwwwww
sixwwwwww 2013년 10월 13일
편집: sixwwwwww 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
Alessandro Masullo 2013년 10월 13일
Yes, this is what I'm trying to do :D I don't want to bug you, but I'm trying to do this with matrix indexing, because I already do this with a loop, but I have very big matrix and the same operation to be done on different matrix of different sizes...
sixwwwwww
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
Jan 2013년 10월 13일

0 개 추천

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!

Translated by