How to rearrange the rows of a matrix so the column values loop

I have a matrix
rec =
[ 0 0
0 0.4
0.2 0
1.0 0.4
1.0 0.9 ]
I want it rearranged so that it appears like so
rec_new =
[ 0.2 0
0 0
0 0.4
1.0 0.9
1.0 0.4 ]
If you take any one of these columns and concatenate it onto itself, the values loop from a minimum to a maximum. How can I get a program to automatically do this for a number of matrices? I'm not sure how to start this, so any help would be appreciated.
Ultimately, I need the pairs of points rearranged so that when they are graphed they form a polygon that does not intersect itself.
Thanks

2 个评论

It took me a bit but I understand what you wanted now, Ill think about it, it seems interesting. Best luck, Ill let you know if I come across something.
Leah and Angus, I'd like to thank you both for your assistance. I appreciate it greatly.

请先登录,再进行评论。

 采纳的回答

Hey that was interesting, I think this is what you want (not sure under what conditions it might fail):
rec = [0 0;0 0.4;0.2 0;1 0.4;1 0.9]
K = convhull(rec);
rec(K,:)
ans =
0 0
0.2000 0
1.0000 0.4000
1.0000 0.9000
0 0.4000
0 0
Came across this via Gift wrapping algorithm and then found convhull from there if you are interested.
It basically draws a line around the outside of the points ... kinda or something :D
Hope this does it for you,
Cheers, Angus.

6 个评论

This is very close to what I want. It works perfectly for 2D problems. I ultimately want to apply this to a set of 3D points. I tried it for my test matrix, and it did work with 2D, no problem. When I applied it to 3D though, I had to do a bit of modification that I am not sure is strictly legal.
Instead of
K = convhull(rec);
I had to do
K = convhull(rec(:,1:2));
rec(K,:)
It does organize them into the correct order this way, even with a third dimension, I'm just not sure what I'm doing is strictly correct. Either way, muchos gracias, Angus.
Good to have a start anyways, it will depend what you want for your 3d problem now. As you have modified it with convhull(rec(:,1:2)) you are just ignoring the 3rd dimension and it becomes as it was for the 2d problem. This may work if you just need the polygon 'lines' or edges to not intersect each other, but im not entirely confident there with it being 3d space, I have not tried to plot any of this. If you use convhull on a 2d problem it returns a single column of point indicies that would result in a convex polygon when connected, if convhull is used on a 3d problem it returns 3 columns of point indices with each row defining a triangle. Unfortunately I have to run off now so I cant test this further but I would think that the result of plotting these triangles is a 3d convex polygon of sorts.
>> K3 = convhull(rec3d)
K3 =
1 2 5
1 3 2
1 4 3
1 5 4
2 3 4
2 4 5
So triangle1 has vertices rec3d(1,:),rec3d(2,:),rec3d(5,:)
I hope this works out. Ill check in tomorrow, best luck.
Angus
The mess with the triangles seems like unnecessary work (I say that, but wait until you see the code I created!). I haven't played with it much though, so I'm still going to look at it.
So, I came up with a dirty way to make this work
for i = 1:size(rec,1)
if rec(:,1) == rec(i,1)
B = convhull(rec(:,2:3));
elseif rec(:,3) == rec(i,3)
B = convhull(rec(:,1:2));
else
B = convhull(rec(:,1),rec(:,3));
end
end
I'm sure this makes you cringe, but it does seem to work for any 3d set of points.
I'm also trying to look at the solution suggested by Leah. I was thinking maybe I could find the centroid of the polygon and then convert to either spherical or polar coordinates and then try to arrange by the angles, though I think this may lead me to the same problem with taking convhull and applying that to a 3d set of points.
Ill get back to you after lunch sometime, busy day today, but I just found this function that might do something nice (havn't even tried it, sorry).
This seems to be an extension of sorts of the convhull function: convhulln
Docs:
~~~~~~~~~~
Plotting the output of convhulln depends on the value of n:
For n = 2, use plot as you would for convhull.
For n = 3, you can use trisurf to plot the output.
The calling sequence is
K = convhulln(X);
trisurf(K,X(:,1),X(:,2),X(:,3))
You cannot plot convhulln output for n > 3.
~~~~~~~~~~
Well for now maybe that helps, hope it is not just the same as convhull in 3d.
hey again, so convhulln seems to be the same as convhull in the 3d case for practical purposes. Using convhull and trisurf as in my previous comment produced a nice 3d plot of a filled polygon, is that what you are aiming for in this case?
In reviewing your original question it seems you might want to actually rearrange the structure of the matrix, is this still necessary since it can be plotted successfully without manipulation?
I do want to rearrange the structure of the matrix. I need to be able to do some calculations with them for the area and perimeter of the polygon the points create

请先登录,再进行评论。

更多回答(1 个)

"If you only want them to be counter-clock wise, why not convert the [x,y] point to polar coordinate and sort them by angle?"

2 个评论

I don't really know that this makes sense in 3D. If you had a mesh making up a sphere what order would you want them to go in? Clockwise direction doesn't mean anything in 3D space. I think you need to rethink your desired outcome in 3D space.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by