randomal
randomal

Reputation: 6592

Long pause after each iteration in Matlab loop

I'm working with a Matlab script which loads a stack of images, processes and them stores them (see below). The script runs fine, but it has long pauses (about one hour for each step) between each step of the main loop. Do you have any suggestion on how to make it run smoothly?

clear;
number_projections = 40; 
number_images_per_projection = 1386; 
offset = 5;
OB=zeros([number_images_per_projection 512 512]);
IM=zeros([number_images_per_projection 512 512]);

m = 0;
for j = 14:number_projections
    for k = 1:number_images_per_projection
      % Lets load the OB images
      t = sprintf('/data/alcer/Data_analysis/BL18_May2014/19_Nickel/Simulated_OB/OB_%03i_%05i.fits',j-1, k-1);
      m = m + 1;
      OB(m,:,:) = fitsread(t);
      % And now the corrected images
      u = sprintf('/data/alcer/Beamtimes/Data_BL18_May2014/MCP_detector/19_Nickel_1/Corrected/Ni_corrected_all/Nickel_1_%03i_%05i.fits',j-1, k-1);
      IM(m,:,:) = fitsread(u);
    end
    %Let's use a rolling median (5 images before and 5 after)

    rolling_median_IM = zeros([512 512]);
    rolling_median_OB = zeros([512 512]);
    clean_images = zeros([512 512]);

    for k = offset+1:number_images_per_projection-offset
        rolling_interval_IM = squeeze(IM(k-offset:1:k+offset,:,:));        
        rolling_interval_OB = squeeze(OB(k-offset:1:k+offset,:,:));
        rolling_median_IM(:,:) = squeeze(median(rolling_interval_IM,1));
        rolling_median_OB(:,:) = squeeze(median(rolling_interval_OB,1));
        clean_images(:,:) = ((squeeze(OB(k,:,:)))./squeeze(rolling_median_OB(:,:)))-((squeeze(IM(k,:,:)))./squeeze(rolling_median_IM(:,:)));
        v = sprintf('/data/alcer/Data_analysis/BL18_May2014/19_Nickel/Cleaned_images/Cleaned_Ni_%03i_%05i.fits',j-1, k-1);
        fitswrite(clean_images, v);
    end
end

Upvotes: 0

Views: 132

Answers (1)

Adrian
Adrian

Reputation: 3306

Try using the profile tool to find out which bit of the code is causing the delay.

Upvotes: 3

Related Questions