# Matlab - Sort into deciles each column

18 views (last 30 days)
V on 4 Dec 2015
Edited: René Wijnen on 30 Jul 2021
Suppose I have a matrix A [m x 1], where m is not necessarily even. I to create a matrix B also [m x 1] which tells me the decile of the elements in A (i.e. matrix B has numbers from 1 to 10).
I know I can use the function sort(A) to get the position of the elements in A and from there I can manually get deciles. Is there another way of doing it?
I think one possibility would be B = ceil(10 * tiedrank(A) / length(A) . What do you think? Are there any issues with this?
Also, more generally, if I have a matrix A [m x n] and I want to create a matrix B also [m x n], in which each column of B should have the decile of the corresponding column in A , is there a way of doing it without a for loop through the columns?
Hope the problem at hand is clear. So far I have been doing it using the sort function and then manually assigning the deciles, but it is very inefficient.
Thank you

jgg on 4 Dec 2015
I think a way to do this is to use the ecdf command (<http://www.mathworks.com/help/stats/ecdf.html>)
• If you take your data and do [F,X] = ecdf(A) then this will compute the empirical distribution of A.
• You then just have to match the values in A to those in X which you can do using ismember like this [~,loc] = ismember(X,X2)
• Finally, your quantiles of the values in A will be f(loc)
• You can then sort them into deciles, or whatever you want at that point by rounding to the appropriate value.

René Wijnen on 30 Jul 2021
Edited: René Wijnen on 30 Jul 2021
I would use the following to split a vector X into deciles:
X_dec = discretize(X,quantile(X,[0:10]/10))
Works as well for each column of an array X
{"ft_lang_label":"__label__en","ft_lang_prob":0.934722,"math_prob":0.86814034,"size":886,"snap":"2021-43-2021-49","text_gpt3_token_len":227,"char_repetition_ratio":0.12471655,"word_repetition_ratio":0.08791209,"special_character_ratio":0.25959367,"punctuation_ratio":0.0882353,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9915472,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-22T18:52:29Z\",\"WARC-Record-ID\":\"<urn:uuid:98507a08-7e7e-4ddd-92e2-831a57c04cbc>\",\"Content-Length\":\"116558\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b72a3500-da84-4267-b84c-3699d9b20efd>\",\"WARC-Concurrent-To\":\"<urn:uuid:ed1880fc-aad6-4a3b-9981-7ffe8a48e0ea>\",\"WARC-IP-Address\":\"104.69.217.80\",\"WARC-Target-URI\":\"https://au.mathworks.com/matlabcentral/answers/258931-matlab-sort-into-deciles-each-column\",\"WARC-Payload-Digest\":\"sha1:BOBT52EUR5YEEH7MBVEZWI33QFMWBDI3\",\"WARC-Block-Digest\":\"sha1:LMDG424NKBRZIABUCW3PPJUFGBH6NQ5R\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585518.54_warc_CC-MAIN-20211022181017-20211022211017-00424.warc.gz\"}"}