function SqDisp = AllCoords_ComputeSD(ScriptVar, AllCoords, N_Particles_sampled)
    % Compute Squared Displacement
    % Output -
    %    SqDisp - n x k array where n = number of particles, k denotes the
    %    total number of frames
    %
    % Inputs - 
    %    ScriptVar - Parameters container map
    %    AllCoords - Array of particle coordinates 
    %    N_Particles_sampled - Number of particles to be sampled for MSD
    %    calculation
    % 
    % Example -
    %   OutputVar = AllCoords_ComputeSD(ScriptVar, AllCoords, 1000)
    
    tic
    TotalParticles = sum(ScriptVar('N_Particles'));
    if ~exist('RequiredFrames','var')
        RequiredFrames = [1 ScriptVar('N_Frames')];
    end
    TotalFrames = RequiredFrames(2)-RequiredFrames(1)+1;
    AllCoords = AllCoords(randperm(TotalParticles,N_Particles_sampled)',:,:);
    SqDisp = zeros(N_Particles_sampled, TotalFrames);
    for i = 1:N_Particles_sampled
        ThisParticleCoords = squeeze(AllCoords(i,1:2,:))';
        SqDisp(i,:) = (ThisParticleCoords(:,1)-ThisParticleCoords(1,1)).^2 + ...
            (ThisParticleCoords(:,2)-ThisParticleCoords(1,2)).^2;
    end
    fprintf("\n********\nSD computation completed\n********\n")
    % For histogram, use this directly. For plotting MSD, calculate mean
    % and SEM for each time frame
    toc
end