Search code examples
arraysmatlabmatlab-figurescatter-plot

scatter plot: tall arrays eating up all the memory


I am working on tone mapping operators in HDR. My question is very simple. I want to scatter plot large arrays on Matlab 2015Ra with computer specs core i5 20GB RAM. Only the scatter plot eats up the whole memory (around 92% of 20GB). I need some suggestion to plot tall arrays. I know Matlab 2018 has binscatter function but I have lower version. Thank you. Sample Code:

a=randn(21026304,1);
scatter(a,a); 

Only this code eats up the all memory.


Solution

  • You can create a binscatter like function yourself with histcounts2! Histcounts bins the data into an NxN array which you can then visualize with imshow... this is pretty memory-efficient as every bin only takes up a couple of bytes, regardless of the input size.

    % Some (correlated) data
    x = randn(1e6,1);
    y = randn(1e6,1)+x;
    
    % Count 32x32 bins
    [N,ax,ay] = histcounts2(x,y,32);
    
    % Some gradient
    cmap = [linspace(1,0,16);
        linspace(1,0.3,16);
        linspace(1,0.5,16)].';
    
    % Show the histogram
    imshow(...
        N,[],...                        % N contains the counts, [] indicated range min-max
        'XData', ax, 'YData', ay, ...   % Axis ticks
        'InitialMagnification', 800,... % Enlarge 8x
        'ColorMap', cmap...             % The colormap
    );
    
    colorbar;       
    axis on;
    title('bin-counts');
    

    Example of custom binned scatter-plot