Function sort and smooth in matlab
Show older comments
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)
Walter Roberson
on 10 Apr 2012
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
Shalini
on 10 Apr 2012
Jan
on 10 Apr 2012
@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...
Shalini
on 10 Apr 2012
Walter Roberson
on 10 Apr 2012
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.
Shalini
on 10 Apr 2012
Jan
on 10 Apr 2012
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.
Shalini
on 10 Apr 2012
Sean de Wolski
on 10 Apr 2012
[1 2 3]
Jan
on 10 Apr 2012
@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?).
Shalini
on 11 Apr 2012
Geoff
on 11 Apr 2012
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'.
Jan
on 11 Apr 2012
@Geoff: Exactly.
Shalini
on 11 Apr 2012
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!