How to look at the source code for MATLAB's built-in sort function?

241 ビュー (過去 30 日間)
Joe
Joe 2013 年 5 月 1 日
コメント済み: Walter Roberson 2020 年 9 月 30 日
I need to find the big O runtime for MATLAB's built-in sort function, but I'm not quite sure how to if I don't even know how the function is coded. Is there some way to look at the code for the function because I know for some built-in functions you are able to look at the source code.

採用された回答

Jonathan Epperl
Jonathan Epperl 2013 年 5 月 1 日
If the source code is available, then
edit sort
will do the job. I can't check right now, but you go ahead and try that.

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2013 年 5 月 1 日
編集済み: Walter Roberson 2013 年 5 月 1 日
To look at the source code, get a job or internship with Mathworks.
sort() is a built-in; you cannot "edit sort" to look at it.
There are a number of routines for which MATLAB calls into LAPACK or BLAS when the arrays are large enough to make it worth the overhead. I do not know if sort() is one of those. If it is, then the LAPACK sort algorithm is at http://www.netlib.org/lapack/explore-html/de/de5/slasrt_8f_source.html
I have doubts about that being the appropriate routine, as it appears to only support a single data-type.

Muhammad Ahsan  Zahid
Muhammad Ahsan Zahid 2020 年 4 月 14 日
編集済み: Muhammad Ahsan Zahid 2020 年 4 月 14 日
If you want to see the sourcee code behind the function, open the function as:
open function_name.m
Example
open rand.m
If available it will show you.
  3 件のコメント
Claudia Romero Rodriguez
Claudia Romero Rodriguez 2020 年 9 月 30 日
Actually it does show the code
Walter Roberson
Walter Roberson 2020 年 9 月 30 日
built-in (/Applications/MATLAB_R2020a.app/toolbox/matlab/datafun/@double/sort) % double method
edit sort will show you only documentation, unless you specifically open, for example, toolbox/matlab/datatypes/categorical/@categorical/sort.m .
But even that one does its work by using categoricalsort which is matlab.internal.categoricalUtils.categoricalsort.categoricalsort which is a build-in method.

サインインしてコメントする。


Chien-Han Su
Chien-Han Su 2019 年 12 月 21 日
I'm not sure whether this is what you need, I would recommend you to record (by tic and toc) and analyze the exceution time to find the Big-O since you can't not get the source code of built-in function, like this
iterStart = 10; % drop the first ten iterate
iterNum = 1000;
N = 1000;
t = zeros(iterNum,N);
for m = iterStart:iterNum
for n = 1:N
a = rand(1,n);
tic;
b = sort(a);
t(m,n) = toc;
end
end
tAvg = mean(t,1);
tAvg = tAvg/tAvg(1); % normalize
plot(1:N,tAvg)
xlabel('n');
ylabel('Normalized Time')
title('Normalized Execution Time for Sorting a Vector');
and you will get
How_to_look_at_the_source_code_for_MATLAB_built_in_sort_function.jpg

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by