MATLAB: Finding the coordinates of multiple midpoints in an imported numerical matrix

calculating midpointfunctionmatrixmidpoint

Hi, I'm new to matlab and have been stuck on this for awhile now.
I have a large numerical matrix dataset i imported into matlab. For the sake of my question we can use the magic(4) matrix.
EDU>> a = magic(4)
a =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
I also found a function which I called and saved as midpoint:
function [mp] = midpoint(x1,y1,x2,y2)
mp = [0 0];
mp(1) = (x1+x2)/2;
mp(2) = (y1+y2)/2;
return
To find a single value in the matrix the function works fine:
EDU>> midpoint((a(1,1)),(a(1,2)),(a(1,3)),(a(1,4)))
ans =
9.5000 7.5000
However the instant I try to calculate more than 1 midpoint at once I get an error:
EDU>> midpoint((a(1:2,1)),(a(1:2,2)),(a(1:2,3)),(a(1:2,4)))
In an assignment A(I) = B, the number of elements in B and I must be the same.In an assignment
Error in midpoint (line 4)
mp(1) = (x1+x2)/2;
In my dataset I easily have to calculate more than 3000 midpoints. Is there a way to fix my current function or an alternative way to do this that allows me to calculate all the midpoints simultaneously
Thanks Kyle

Best Answer

Try this:
function test3
a =[...
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1]
m = midpoint(a) % Call the function
function [mp] = midpoint(arrayWith4Columns)
x1 = arrayWith4Columns(:, 1);
y1 = arrayWith4Columns(:, 2);
x2 = arrayWith4Columns(:, 3);
y2 = arrayWith4Columns(:, 4);
mp = [x1+x2, y1+y2]/2;
return
It gets the midpoint for all rows in the 4-column matrix.