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?
답변 (1개)
Walter Roberson
2012년 4월 10일
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
Shalini
2012년 4월 10일
Jan
2012년 4월 10일
@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
2012년 4월 10일
Walter Roberson
2012년 4월 10일
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
2012년 4월 10일
Jan
2012년 4월 10일
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
2012년 4월 10일
Sean de Wolski
2012년 4월 10일
[1 2 3]
Jan
2012년 4월 10일
@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
2012년 4월 11일
Geoff
2012년 4월 11일
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
2012년 4월 11일
@Geoff: Exactly.
Shalini
2012년 4월 11일
카테고리
도움말 센터 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!