average of each two vectors of a matrix

조회 수: 10 (최근 30일)
Niki
Niki 2014년 10월 9일
댓글: Stephen23 2014년 10월 9일
I have a matrix of n*n for example 10*5
x=rand(10,5)
Then I want to get the average of each two row. For example the first two row (each element ) etc
I will have a matrix of 5*5 after that

채택된 답변

Iain
Iain 2014년 10월 9일
(x(2:2:10,:) + x(1:2:9,:))/2
Thats the "simple" answer. There are other methods that are more flexible, but which need a little more thought eg.:
y = reshape(x,[2,5,5])
answer = squeeze(mean(y));
  댓글 수: 1
Stephen23
Stephen23 2014년 10월 9일
The first solution can also be generalized a little using end instead of hardcoded limits:
(x(2:2:end,:) + x(1:2:end,:))/2

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 10월 9일
편집: Azzi Abdelmalek 2014년 10월 9일
a=reshape(mean(permute(reshape(x',5,2,[]),[2 1 3])),5,5)'

카테고리

Help CenterFile 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