【问题标题】:Can this piece of matlab script be vectorized further?这段matlab脚本可以进一步向量化吗?
【发布时间】:2010-07-21 04:27:01
【问题描述】:

所以我试图用这段代码做的是找到图像线上低于某个阈值的所有像素。然而,问题是这段代码是在一个双 for 循环中执行的(是的,我知道 :( ),每个像素一次,所以它非常慢。我想知道我是否还能做任何其他事情。

一些技巧会很棒,因为我对 MATLAB 优化还很陌生,而且我只知道基础知识(尽量不要使用循环,或者在内部函数中多次调用脚本等)。如果这不起作用,我可能不得不求助于 MEX 文件,这对于我小组中的其他研究人员来说将更难维护。谢谢!

for y = 1:y_len
    for x = 1:x_len
        %//...do stuff to calc slope and offset for the line, 
                %//this can be vectorized pretty easily.

        yIndices = xIndices.*slope + offset;
        yIndices = round(yIndices);

        yIndices = yIndices + 1;
        xIndices = xIndices + 1;
        valid_points = (yIndices <= 308) & (yIndices > 0);

        %this line is bottle necking----------------------------------------
        valid_points = yIndices(valid_points)+(xIndices(valid_points)-1)*308;
        %-------------------------------------------------------------------

        valid_points = valid_points(phaseMask_R(valid_points));
        t_vals = abs(phase_R(valid_points)-currentPhase);
        point_vals = [XsR(valid_points);YsR(valid_points)] - 1;
        matchedPtsCoordsR = point_vals(:,(t_vals<phaseThreshold) |(abs(192-t_vals)<phaseThreshold));

        matchedIndex = size(matchedPtsCoordsR,2);
        if(matchedIndex ==0)
          continue
        end

        centersMinMaxR = zeros(1,matchedIndex);
        cmmIndexR = 1;
        for a = 1:matchedIndex;
          if(a==1)
            avgPosition = matchedPtsCoordsR(:,a);
            centersMinMaxR(1,1) =1;
          else
            currentPosition = matchedPtsCoordsR(:,a);


            %also very slow----------------------------------------------
            distance = sum(abs(currentPosition-avgPosition));
            %------------------------------------------------------------
            if(distance>4) % We are now likely in a different segment.
              centersMinMaxR(2,cmmIndexR) = a-1;
              cmmIndexR = cmmIndexR + 1;
              centersMinMaxR(1,cmmIndexR) = a;
            end
            avgPosition = matchedPtsCoordsR(:,a);
          end
        end

        centersMinMaxR(2,cmmIndexR) = a;
        centersR = round(sum(centersMinMaxR)/2);

        %//...do stuff with centersR
                    %//I end up concatenating all the centersR into a 
                    %//large vector arrray with the start and end of 
                    %//each segment.

【问题讨论】:

    标签: optimization matlab vectorization


    【解决方案1】:

    首先,MatLab Profiler 是您最好的朋友,我假设您知道它,因为您知道什么是瓶颈。

    删除双循环的快速修复方法是使用: 命令。您可以使用单循环而不是使用双循环,但要沿整个维度计算每个行或列索引。举个简单的例子:

    m = magic(2);
    slope = 5;
    
    m =
         1     3
         4     2
    
    m(1,:) * slope  =
         5    15
    
    m(:,1) * slope =
         5
        20
    

    不要使用交错数组,而是使用稀疏数组。 Matlab 内置了对它们的支持:

    Matlab Create Sparse Array

    Matlab Sparse Matrix Operations

    更新

    关于使用稀疏数组与普通数组的优缺点: Sparse vs Normal Array Matlab

    稀疏矩阵是真正的福音 真正使用稀疏的人 矩阵,但 25% 的非零值很简单 不够“稀疏”以获取任何收益 大多数情况。

    寻找更多更新,因为我有更多时间查看您的代码:p

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多