Function sort and smooth in matlab

1) Consider the function sort:
You can call it with as below;
B = sort(A)
B = sort(A,dim)
If A is a vector, do I need the argument dim?
2)Now, consider the function smooth;
suppose I suply bot the independent variable x and dependent variable y as below;
yy = smooth(x,y,...)
Is it mandatory that I should be sorting all values of x in acending order before I plot?I mean does smooth carry out smoothing with x arrranged in ascending order?

Answers (1)

1) If you are sorting a vector, you need to supply dim if you care about which direction the vector is sorted on.
In particular, it is possible that you expect your A to be a full array but there are conditions under which you might possibly get out a vector (e.g., a single row when you expect many rows.) If you do not specify the direction the sort could accidentally be a direction you did not expect.
yy = smooth(x,y,...) additionally specifies x data. If x is not provided, methods that require x data assume x = 1:length(y). You should specify x data when it is not uniformly spaced or sorted. If x is not uniform and you do not specify method, lowess is used. If the smoothing method requires x to be sorted, the sorting occurs automatically.
Note that final sentence.

13 Comments

Thanks for the reply..
1)What do you exactly mean when you say "you need to supply dim if you care about which direction vector is sorted on"? Can you give a simple example too illiustrate this?
2)The last sentence says:If the smoothing method requires x to be sorted, the sorting occurs automatically- isn't this better -i.e. automatically if x is required to be sorted it is done.Rught?In that case why do we supply x ?
@Shalini: If you supply a matrix to the SORT command without specifying the dimension to operate on, it is sorted along the first non-singelton dimension:
x = rand(2,3); disp(sort(x, 1)); disp(sort(x, 2)); disp(sort(x));
If you want to sort along the first dimension, but it has the length 1 for anby reasons, using sort(x) would unintentionally sort along the 2nd dimension.
If the data must be sorted for a specific procedure, it can be helpful to sort it explicitely, because this might support the debugging. Even if you waste 0.01 seconds for the runtime, it might save you an hour of debug-time. Even asking this question will need more time than adding an explicite SORT in the code...
Thanks but as I said, I have a vector not a matrix-so there is no question of sorting along a particular dimension- right?
Well, you _might_ want to sort along the other direction for some reason. But if you *want* the first non-singleton dimension to be the one that sorting is done on, then you can leave off the dimension.
Which other direction you mean?You mean resolving the vector in some other direction?
You can sort [3,1,2] along the first dimension by: sort([3,1,2], 1). The result is [3,1,2] without any changes, but this is a correctly sorted vector - along the 1st dimension.
If I would like to sort [3,1,2] along the second dimension, what would be the answer?
@Shalini: Something is going wrong in this discussion. Do you know the documentation "help sort" and "doc sort"? You can find out the result of sorting [3,1,2] by your own. Simply try it in the command line.
The specification of the dimension to operate on is a typical of Matlab. Therefore I suggest to read the Getting Started chapters (again?).
@Jan Simon.
Sorry Jan-if it pppeared that something is wrong with the discussion.My problem is undestanding fundamentally here.I believe sorting is arranging in ascending order (or descending order).Right?
I do not follow what is meant by sorting in a poarticular direction for a vector.What is meant by 'sorting along a DIRECTION'?
Jan and Walter mean 'direction' as left-right versus up-down. They are saying that if you always want to sort the columns of a matrix, but then by chance that matrix happens to have a single row, then sort will behave differently if you do not specify the dimension. So you could take 'direction' in this case to mean 'dimension'.
@Geoff: Exactly.
@Geoff,Jan,Walter:
Please correct if I'm wrong and sorry if I'm fundamentally wrong.
We can sort with either dimension =1 or diemnsion =2.
When dimension =1 (during sort) the function 'sort', just arranges all the columns in ascending order.
When dimension =2 (during sort) the function 'sort', just arranges all rows in ascending order.
This is what (I think) is happening in the examples in this link:
http://www.mathworks.in/help/techdoc/ref/sort.html

Sign in to comment.

Categories

Asked:

on 10 Apr 2012

Community Treasure Hunt

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

Start Hunting!