<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242806</link>
    <title>MATLAB Central Newsreader - sort large amount of data</title>
    <description>Feed for thread: sort large amount of data</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 21 Jan 2009 09:07:01 -0500</pubDate>
      <title>sort large amount of data</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242806#622896</link>
      <author>Theodor Zouk</author>
      <description>Hi &lt;br&gt;
I have very large amount of data (reell values of class double) that is registrered over the chronological appearence and saved in many .mat files. I want to sort this data in a ascending order. Now, SORT do this, but it can only do it per each .mat-file. The ideal would be if all the .mat files could be saved into one .mat file and then loaded into workspace and then sorted. But this can not be done when there is not enough memory. Any tips how i should 'loop' through all the mat files and then at the end have many mat files where the maximum value in one mat file is lower than the minimum value of the next consecutive mat file.&lt;br&gt;
&lt;br&gt;
Best regards &lt;br&gt;
Theo   </description>
    </item>
    <item>
      <pubDate>Wed, 21 Jan 2009 10:27:10 -0500</pubDate>
      <title>Re: sort large amount of data</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242806#622908</link>
      <author>Rune Allnor</author>
      <description>On 21 Jan, 10:07, &quot;Theodor Zouk&quot; &amp;lt;reb...@hotmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; Hi&lt;br&gt;
&amp;gt; I have very large amount of data (reell values of class double) that is r=&lt;br&gt;
egistrered over the chronological appearence and saved in many .mat files. =&lt;br&gt;
I want to sort this data in a ascending order. Now, SORT do this, but it ca=&lt;br&gt;
n only do it per each .mat-file. The ideal would be if all the .mat files c=&lt;br&gt;
ould be saved into one .mat file and then loaded into workspace and then so=&lt;br&gt;
rted. But this can not be done when there is not enough memory. Any tips ho=&lt;br&gt;
w i should 'loop' through all the mat files and then at the end have many m=&lt;br&gt;
at files where the maximum value in one mat file is lower than the minimum =&lt;br&gt;
value of the next consecutive mat file.&lt;br&gt;
&lt;br&gt;
Two ideas come to mind:&lt;br&gt;
&lt;br&gt;
1) Loop through each file and build a *total* histogram&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;of the values that appear in the total data set.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;Use this histogram to separate the data set by value&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;into intermediate files. Sort these files, and merge.&lt;br&gt;
2) Find a copy of Knut's &quot;The art of Computer Programming:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;Sorting and Searching&quot; and read.&lt;br&gt;
&lt;br&gt;
Rune</description>
    </item>
    <item>
      <pubDate>Wed, 21 Jan 2009 18:23:02 -0500</pubDate>
      <title>Re: sort large amount of data</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242806#623035</link>
      <author>Roger Stafford</author>
      <description>&quot;Theodor Zouk&quot; &amp;lt;rebet4@hotmail.com&amp;gt; wrote in message &amp;lt;gl6ojl$1ag$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi &lt;br&gt;
&amp;gt; I have very large amount of data (reell values of class double) that is registrered over the chronological appearence and saved in many .mat files. I want to sort this data in a ascending order. Now, SORT do this, but it can only do it per each .mat-file. The ideal would be if all the .mat files could be saved into one .mat file and then loaded into workspace and then sorted. But this can not be done when there is not enough memory. Any tips how i should 'loop' through all the mat files and then at the end have many mat files where the maximum value in one mat file is lower than the minimum value of the next consecutive mat file.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Best regards &lt;br&gt;
&amp;gt; Theo&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;In merge sorting for example, the basic step is a comparison between the next two elements of two arrays to see which is smaller, with the smaller being selected for insertion in another array and being removed (in a sense) from the original array.  Assuming the two original arrays were already sorted, that results in a single combined array that is sorted.  If this is repeatedly performed, sorted arrays starting from single elements and working up to an entire large sorted array can be obtained.  It is an efficient order n*log2(n) algorithm.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;It seems to me that you can emulate such a procedure with your .mat files being considered as analogous to individual elements within &quot;superarrays&quot; which are not stored in memory simultaneously.  Starting with all the .mat files being sorted within themselves, you can bring in two .mat sorted files, merge them into two equally large .mat files which are mutually sorted in the sense that each element of one is less than each element of the other.  Now you have the analogy to two sorted elements.  By continuing this you eventually have all .mat files paired off into mutually sorted pairs.  The next stage is to do the same kind of &quot;supermerging&quot; to obtain sets of four .mat files which are mutually sorted, always taking care not to have more than four .mat files in computer memory at any one time.  Eventually all the .mat files will represent one entire sorted array, though of course it will be too large to be stored in memory.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;The sizes of these .mat files are dictated by the requirement that no more than four of them will ever be allowed to reside in memory at the same time.  Obviously there is the extra overhead of reading and writing numerous .mat files, but hopefully this should be no more time-consuming and maybe a lot less than the internal sorting of very long .mat file pairs.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I hope all that makes sense to you, Theo.  I can envision it more easily than I can explain it.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Wed, 21 Jan 2009 21:21:12 -0500</pubDate>
      <title>Re: sort large amount of data</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242806#623082</link>
      <author>Peter Boettcher</author>
      <description>Rune Allnor &amp;lt;allnor@tele.ntnu.no&amp;gt; writes:&lt;br&gt;
&lt;br&gt;
&amp;gt; On 21 Jan, 10:07, &quot;Theodor Zouk&quot; &amp;lt;reb...@hotmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt;&amp;gt; Hi I have very large amount of data (reell values of class double)&lt;br&gt;
&amp;gt;&amp;gt; that is registrered over the chronological appearence and saved in&lt;br&gt;
&amp;gt;&amp;gt; many .mat files. I want to sort this data in a ascending order. Now,&lt;br&gt;
&amp;gt;&amp;gt; SORT do this, but it can only do it per each .mat-file. The ideal&lt;br&gt;
&amp;gt;&amp;gt; would be if all the .mat files could be saved into one .mat file and&lt;br&gt;
&amp;gt;&amp;gt; then loaded into workspace and then sorted. But this can not be done&lt;br&gt;
&amp;gt;&amp;gt; when there is not enough memory. Any tips how i should 'loop' through&lt;br&gt;
&amp;gt;&amp;gt; all the mat files and then at the end have many mat files where the&lt;br&gt;
&amp;gt;&amp;gt; maximum value in one mat file is lower than the minimum value of the&lt;br&gt;
&amp;gt;&amp;gt; next consecutive mat file.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Two ideas come to mind:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; 1) Loop through each file and build a *total* histogram&lt;br&gt;
&amp;gt;    of the values that appear in the total data set.&lt;br&gt;
&amp;gt;    Use this histogram to separate the data set by value&lt;br&gt;
&amp;gt;    into intermediate files. Sort these files, and merge.&lt;br&gt;
&amp;gt; 2) Find a copy of Knut's &quot;The art of Computer Programming:&lt;br&gt;
&amp;gt;    Sorting and Searching&quot; and read.&lt;br&gt;
&lt;br&gt;
Adding a few thoughts:&lt;br&gt;
&lt;br&gt;
Do you absolutely need a full sort?  Could you stop at a histogram like&lt;br&gt;
Rune suggests?&lt;br&gt;
&lt;br&gt;
Do you have a priori knowledge of the range of the values, or even&lt;br&gt;
better the approximate distribution?  If you know the distribution, you&lt;br&gt;
can skip the empirical histogram step and just toss all the values into&lt;br&gt;
bins that you determine from your known distribution.&lt;br&gt;
&lt;br&gt;
I would use raw binary files for the scratch space.  There will be much&lt;br&gt;
less overhead than MAT files, and seemless appending of new values.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
% Skip this if you can say something about your distribution&lt;br&gt;
for n=1:num_files&lt;br&gt;
&amp;nbsp;&amp;nbsp;% read file n&lt;br&gt;
&amp;nbsp;&amp;nbsp;% build histogram, accumulate into &quot;running&quot; histogram.  bins must&lt;br&gt;
&amp;nbsp;&amp;nbsp;%  match&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
% choose number M of scratch files, set &quot;edges&quot; that separate files&lt;br&gt;
%  equally&lt;br&gt;
%  (see &quot;histogram equalization&quot;, an image processing technique, for&lt;br&gt;
%  help here)&lt;br&gt;
&lt;br&gt;
for m=1:M&lt;br&gt;
&amp;nbsp;&amp;nbsp;% fopen scratch files&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
for n=1:num_files&lt;br&gt;
&amp;nbsp;&amp;nbsp;% read file n&lt;br&gt;
&amp;nbsp;&amp;nbsp;% separate into file bins (use histc)&lt;br&gt;
&amp;nbsp;&amp;nbsp;% append data sets to all M scratch files&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
for m=1:M&lt;br&gt;
&amp;nbsp;&amp;nbsp;% rewind and read full file m&lt;br&gt;
&amp;nbsp;&amp;nbsp;% sort&lt;br&gt;
&amp;nbsp;&amp;nbsp;% output&lt;br&gt;
&amp;nbsp;&amp;nbsp;% close file m&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
-Peter</description>
    </item>
    <item>
      <pubDate>Fri, 23 Jan 2009 09:55:04 -0500</pubDate>
      <title>Re: sort large amount of data</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242806#623398</link>
      <author>Theodor Zouk</author>
      <description>Peter Boettcher &amp;lt;boettcher@ll.mit.edu&amp;gt; wrote in message &amp;lt;muy63k8s6d3.fsf@G99-Boettcher.llan.ll.mit.edu&amp;gt;...&lt;br&gt;
&amp;gt; Rune Allnor &amp;lt;allnor@tele.ntnu.no&amp;gt; writes:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; On 21 Jan, 10:07, &quot;Theodor Zouk&quot; &amp;lt;reb...@hotmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; Hi I have very large amount of data (reell values of class double)&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; that is registrered over the chronological appearence and saved in&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; many .mat files. I want to sort this data in a ascending order. Now,&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; SORT do this, but it can only do it per each .mat-file. The ideal&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; would be if all the .mat files could be saved into one .mat file and&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; then loaded into workspace and then sorted. But this can not be done&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; when there is not enough memory. Any tips how i should 'loop' through&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; all the mat files and then at the end have many mat files where the&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; maximum value in one mat file is lower than the minimum value of the&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; next consecutive mat file.&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Two ideas come to mind:&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; 1) Loop through each file and build a *total* histogram&lt;br&gt;
&amp;gt; &amp;gt;    of the values that appear in the total data set.&lt;br&gt;
&amp;gt; &amp;gt;    Use this histogram to separate the data set by value&lt;br&gt;
&amp;gt; &amp;gt;    into intermediate files. Sort these files, and merge.&lt;br&gt;
&amp;gt; &amp;gt; 2) Find a copy of Knut's &quot;The art of Computer Programming:&lt;br&gt;
&amp;gt; &amp;gt;    Sorting and Searching&quot; and read.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Adding a few thoughts:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Do you absolutely need a full sort?  Could you stop at a histogram like&lt;br&gt;
&amp;gt; Rune suggests?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Do you have a priori knowledge of the range of the values, or even&lt;br&gt;
&amp;gt; better the approximate distribution?  If you know the distribution, you&lt;br&gt;
&amp;gt; can skip the empirical histogram step and just toss all the values into&lt;br&gt;
&amp;gt; bins that you determine from your known distribution.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I would use raw binary files for the scratch space.  There will be much&lt;br&gt;
&amp;gt; less overhead than MAT files, and seemless appending of new values.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Skip this if you can say something about your distribution&lt;br&gt;
&amp;gt; for n=1:num_files&lt;br&gt;
&amp;gt;   % read file n&lt;br&gt;
&amp;gt;   % build histogram, accumulate into &quot;running&quot; histogram.  bins must&lt;br&gt;
&amp;gt;   %  match&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % choose number M of scratch files, set &quot;edges&quot; that separate files&lt;br&gt;
&amp;gt; %  equally&lt;br&gt;
&amp;gt; %  (see &quot;histogram equalization&quot;, an image processing technique, for&lt;br&gt;
&amp;gt; %  help here)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for m=1:M&lt;br&gt;
&amp;gt;   % fopen scratch files&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for n=1:num_files&lt;br&gt;
&amp;gt;   % read file n&lt;br&gt;
&amp;gt;   % separate into file bins (use histc)&lt;br&gt;
&amp;gt;   % append data sets to all M scratch files&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for m=1:M&lt;br&gt;
&amp;gt;   % rewind and read full file m&lt;br&gt;
&amp;gt;   % sort&lt;br&gt;
&amp;gt;   % output&lt;br&gt;
&amp;gt;   % close file m&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -Peter&lt;br&gt;
&lt;br&gt;
Thank you all for the tips and information!&lt;br&gt;
&lt;br&gt;
Answers to some question:&lt;br&gt;
&lt;br&gt;
&amp;gt; Do you absolutely need a full sort?&lt;br&gt;
yes I do.&lt;br&gt;
&lt;br&gt;
&amp;gt; Do you have a priori knowledge of the range of the values, or even&lt;br&gt;
&amp;gt; better the approximate distribution? &lt;br&gt;
Yes and no. I can get the min and max value for the entire data. I assume the distribution is unknown. Though it can be approximated (but it takes time).&lt;br&gt;
&lt;br&gt;
My solution is this:&lt;br&gt;
&lt;br&gt;
Instead of saving the data to many .mat files Im saving them into one binary file (FOPEN,FWRITE). &lt;br&gt;
Im using MEMMAPFILE to be able to index this large file (~5GB). I will then take the median from the data range/intervall and set it as an threshold, loop through the data and save the sorted data into two new files. Now, I now for instance that SORT  function can be applied through MEMMAPFILE function at an certain size of x number of MB of data (depending on the platform). So when making these new files Im also testing them if they are less or equal of x MB. if so the file is sorted. If not, the file is searched for its min and max value and a new threshold is set and two new files are made and tested and so on.&lt;br&gt;
&lt;br&gt;
I dont know if it is faster to first approximate the distribution and then select the threshold(s)? &lt;br&gt;
&lt;br&gt;
Thanks again for your tips!&lt;br&gt;
Best regards&lt;br&gt;
Theo</description>
    </item>
  </channel>
</rss>

