MATLAB代码的矢量化以优化速度

时间:2016-11-30 22:38:39

标签: matlab vectorization

嘿,MATLAB用户。以下代码用于分析电影中的动作。但是,当我在电影上运行脚本时,MATLAB运行代码需要大约14个小时。我听说你可以通过矢量化你的代码来优化运行速度;但是,我不知道如何使用下面的代码来解决这个问题,因为我离程序员很远。

任何人都可以帮助我,或指出我正确的方向吗?

非常感谢 - m.code如下:

 %calculates correlations in sequences of film shots   22 May 2014
     %t = input vector of initial frames of all shots in the film

 prompt1 = 'movie file = ';
 movieavi = input(prompt1,'s');

 mov = VideoReader(movieavi);

 nshots = length(t)-1;

 for n = 1:nshots
     first = t(n);
     last = t(n+1)-2;
     slength = last - first + 1;
     tempcor = 0;

 for k = 1:(slength-1)
     im1 = read(mov,first+k-1);           %read the image
     im2 = read(mov,first+k+1);
     im1 = rgb2gray(im1);               %convert to grayscale
     im2 = rgb2gray(im2);
     tmp = corr2(im1,im2);
     if(isnan(tmp) == 1) 
        tmp = .9999;
     end
     tempcor = tempcor + tmp;
 end

 dataout(n,3) = tempcor/(slength-1);    %mean Lag 1 correlation within a shot
 dataout(n,1) = uint32(first/100);      %frame in hundreds. Can't export 6 digit integer
 dataout(n,2) = mod(first,100);         %modolus 100, to be combined with above

 cycle = t(n)

 end;

 xlswrite('dataout.xls', dataout) %this file appears in matlab folder and must 
 save dataout;                    %the Matlab version of same

0 个答案:

没有答案