Finding the coordinates of multiple midpoints in an imported numerical matrix
5 views (last 30 days)
Show older comments
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
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.
Accepted Answer
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)
See Also
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!