logical indexing for row-wise average

How can I get a row-wise mean of this matrix
1 2 3
A = 4 5 6
7 8 9
but I only want to select these entries
0 1 1
B = 1 1 0
1 0 1
so the result would be something like
2.5
C = 4.5
8.0
Actually, this is easily possible with a loop, but I'd prefer a 1-line code. (this one is just a question of beauty :-))

 채택된 답변

Matt J
Matt J 2013년 3월 28일

1 개 추천

C=sum(A.*B,2)./sum(B,2);

댓글 수: 1

Vincent
Vincent 2013년 3월 28일
thanks, quite clever using .* instead of indexing, thank you! :-)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

태그

질문:

2013년 3월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by