MATLAB Answers

average of each two vectors of a matrix

71 views (last 30 days)
Niki
Niki on 9 Oct 2014
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

  0 Comments

Sign in to comment.

Accepted Answer

Iain
Iain on 9 Oct 2014
(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 Comment

Stephen Cobeldick
Stephen Cobeldick on 9 Oct 2014
The first solution can also be generalized a little using end instead of hardcoded limits:
(x(2:2:end,:) + x(1:2:end,:))/2

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 9 Oct 2014
Edited: Azzi Abdelmalek on 9 Oct 2014
a=reshape(mean(permute(reshape(x',5,2,[]),[2 1 3])),5,5)'

  0 Comments

Sign in to comment.


Translated by