This video covers how to sort a matrix, either sorting each column independently or sorting such that the original rows stay together. Most new users are able to find the SORT command without much problem, but the very useful SORTROWS is often missed.
I need to sort the pixel intensity values of an image in descending order, but then also be able to keep track of each intensities original coordinate in the image. I assume I will need to create an Nx3 matrix where N is the total number of pixels and the 3 column values are: 1. the pixel intensity, 2. the original col, and 3. the row coordinate.
Doug,
I have a set of coordinates for the perimeter of an object in random order. I want to plot these using the statement
plot(X, Y, '-');
so that I get a continuous line plot of the object perimeter. This requires getting the x coordinates representing the top half of the object in increasing order, but the x coordinates for the bottom half of the object in decreasing order.
I have tried to get the coordinates sorted using dummy columns in a matrix using code such as below and using sortrows:
This doesn’t quite work and I get a horizontal line across the middle of my object. I feel I am close using this approach, but I just can’t get it. Any suggestions?
Thanks.
Fred
Good tip! Just what I was looking for. Thanks.
Hi,I want to sort the coordiantes.How to do this?
Eg- 1 0
3 2
21 0
4 4
2 3
5 4
so that the sorted coordinates come out to be:
1 0
21 0
3 2
2 3
4 4
5 4
Mayank,
Please watch the video again. You will see Sortrows at the end. This is the function you are looking for.
Doug
I am getting this error on using simple sort function on the same magic square as your.
??? Index exceeds matrix dimensions.
How can I “sort” this problem ;-)
Sanchay,
You need to show all the relevant code to get help.
Doug
Hi, is there any function that does the same as sortrows but for columns?
Thx for your time!
Renato,
What you need to do is use the transpose operator (‘) to transpose the rows to columns. Then you can use sort rows and transpose right back.
Doug
Hello,
I need to sort the pixel intensity values of an image in descending order, but then also be able to keep track of each intensities original coordinate in the image. I assume I will need to create an Nx3 matrix where N is the total number of pixels and the 3 column values are: 1. the pixel intensity, 2. the original col, and 3. the row coordinate.
Could you help me to get started doing this?
Thanks,
Mark
@Mark
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sort.html
Use sort with the second output argument. That gives the absolute index, but you can use IND2SUB to get back to row and column.
-Doug
Doug,
I have a set of coordinates for the perimeter of an object in random order. I want to plot these using the statement
so that I get a continuous line plot of the object perimeter. This requires getting the x coordinates representing the top half of the object in increasing order, but the x coordinates for the bottom half of the object in decreasing order.
I have tried to get the coordinates sorted using dummy columns in a matrix using code such as below and using sortrows:
This doesn’t quite work and I get a horizontal line across the middle of my object. I feel I am close using this approach, but I just can’t get it. Any suggestions?
Thanks.
Fred
From the convhull doc
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/convhull.html
This should get you where you are going.