2 views (last 30 days)

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

Angus
on 19 Jun 2013

Edited: Angus
on 19 Jun 2013

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.

Angus
on 20 Jun 2013

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).

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.

Angus
on 21 Jun 2013

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?

Leah
on 19 Jun 2013

Leah
on 20 Jun 2013

Opportunities for recent engineering grads.

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/79605-how-to-rearrange-the-rows-of-a-matrix-so-the-column-values-loop#comment_155912

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/79605-how-to-rearrange-the-rows-of-a-matrix-so-the-column-values-loop#comment_155912

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/79605-how-to-rearrange-the-rows-of-a-matrix-so-the-column-values-loop#comment_156031

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/79605-how-to-rearrange-the-rows-of-a-matrix-so-the-column-values-loop#comment_156031

Sign in to comment.