shawnthunt

Archive for January, 2009

Matlab : Sort all rows of a matrix by a certain column

In Uncategorized on 12 January, 2009 at 14:39 pm

From: http://kb.iu.edu/data/afrd.html

In Matlab, how can I sort all of the rows of a matrix according to one of the columns?

If you wish to sort all the rows of a matrix in Matlab according to one of the columns, the general syntax for the command to use is:

[OldRowNumber, NewRowNumber] = sort(OldMatrix(:,SortColumn)); NewMatrix = OldMatrix(NewRowNumber,:);

Suppose, for example, you would like to rearrange the example matrix below (Figure 1) according to the value of the third column, so that your results would be as shown in Figure 2:

Figure 1

A = [1 2 3 3 0 9 6 5 4]

Figure 2

B = [1 2 3 6 5 4 3 0 9]

You would use the following command:

[x, i] = sort(A(:,3)); B = A(i,:);

Matlab :: Get Clicks on an Image

In Uncategorized on 5 January, 2009 at 10:23 am

This was taken from: http://users.ox.ac.uk/~scro1300/mosaic.htm

function

[X, Y] = ginput2(n)

X = []; Y = [];

fori=1:n

[px py ] = ginput(1);

X = [X ; px];

Y = [Y ; py];

plot( px, py, ‘r+’);

text_handle = text( px+5, py, num2str(i) );

set(text_handle,‘Color’,[0 1 0])

end