Finding the coordinates of multiple midpoints in an imported numerical matrix

5 views (last 30 days)
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
  2 Comments
John D'Errico
John D'Errico on 5 Jan 2015
Edited: John D'Errico on 5 Jan 2015
If you don't write vectorized code, why would you expect it to be smart enough to know that you want it to work in a vectorized form? It is not clear what you are really trying to do in the end, but I might suggest interp2 will do what you want in one call.
Kyle
Kyle on 5 Jan 2015
@John, I'm really new to this all which is why I didn't write in vectorized. Basically I have a large dateset that I'm working with 302x10 in one file of many. from columns 3 and 4 is one set of xy coordinates. columns 7 and 8 are the second set of xy coordinates. All I want to do is find the midpoint between these two coordinates and have matlab list it them out to me.
In the question I asked I tried to make it simpler, and show my steps to this final goal. I'm looking into your suggestion and not sure if I'm advanced enough to do it. This is also why I didn't write vectorized code aha.
Sorry for the confusion - Kyle

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 5 Jan 2015
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.

More Answers (0)

Categories

Find more on Environment and Clutter 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!