How do I find the midpoint (or average) of every row in a matrix in increments of two?

68 views (last 30 days)
I need to find the midpoint between every 2 points of every row of a matrix. I need a program that will do it all at once so I will not have to write out an individualized code for every increment that the midpoint is needed, especially since the matrices that I am working with the number of rows range from 38-174. ie if I had the matrix [a, A, AA, b] I need a program that will without writing 3 lines of code will give me the matrix [(a+A)/2, (A+AA)/2, (AA+b)/2] in one step.
The first program I had
Amidpt=NaN*ones(39,2);
Amidpt(2:38,1) = ((A(:,2)+A(:,2))/2);
Amidpt(2:38,2) = ((A(:,3)+A(:,3))/2);
but this gave me the same numbers as matrix A.
The second I tried was
Amidpt=NaN*ones(39,2);
Amidpt(2:38,1) = ave(A(:,2));
Amidpt(2:38,2) = ave(A(:,3));
but this program just gave me the average of the whole column 36 times in rows 2:38.
Any help or ideas would be helpful,
Thank you

Accepted Answer

Walter Roberson
Walter Roberson on 27 Dec 2013
(A(:,1:end-1) + A(:,2:end)) / 2

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!