回 帖 发 新 帖 刷新版面

主题:加快循环的计算速度

我的这个循环里有主要是3个判断,每个判断里还有其他的判断什么的.
各判断里的计算牵扯到其他的两个.所以不知道怎么把这个循环简化.
各位有时间的话帮忙看看出出逐一吧..
下面是这个判断:



for i = prelude:samples
    
    command_old = command;
    tpencode_old = tpencode;
    
    now = PS.time(i);
    command = PS.command(i);
    tpencode = PS.tp_encode(i);
    
    if (((command ~= command_old) & ((now - stamp) >= xt_total)) | (i == (samples - ctsm)))
    % Here all the standard paramaters are consolodated
        
        % Because of command change synchronization with encoder, the last few points are deleted.
        p = max((p - ctbk), 7);
        c(x) = max((c(x) - ctbk), 1);
        
        PS.mark_fault(j) = 0;
        PS.mu_error(j) = mean(error_adj(1:p));
        PS.sigma_error(j) = std(error_adj(1:p),1);
        
        muerr_old = PS.mu_error(j);
        
        if ((PS.sigma_error(j) > sigma_good) ...
        | (PS.time_delay(j) > xt_delay) ...
        | (PS.time_rise(j) > xt_rise) ...
        | (PS.time_total(j) > xt_total))
        % Any of the SDS conditions is an error in the report.
        
            PS.mark_fault(j) = 1;
            q = q + 1;
            
            PS.report(q,1) = PS.time_stamp(j);
            PS.report(q,2) = PS.command_grp(j);
            PS.report(q,3) = PS.change_grp(j);
            PS.report(q,4) = PS.time_delay(j);
            PS.report(q,5) = PS.time_rise(j);
            PS.report(q,6) = PS.time_total(j);
            PS.report(q,7) = PS.mu_error(j);
            PS.report(q,8) = PS.sigma_error(j);
        end
        
        % Because synchronization with encoder, the last points are deleted.
        n = max((n - 5), 4);
        PS.error_bar_grp{j} = error_bar_grp(1:n);
        PS.rate_bar_grp{j} = rate_bar_grp(1:n);
        
        % Next command step prepared.
        j = j + 1;
        k = 1;
        n = 0;
        p = -1;
        x = max((min(floor(command/8.0), 9) + 1), 1);
        
        error_adj = filler;
        error_bar_grp = filler;
        rate_bar_grp = filler;
        change = command - command_old;
        leave = command_old + muerr_old;
        stamp = now;
        
        PS.number_group = j;
        PS.command_grp(j) = command;
        PS.change_grp(j) = change;
        PS.time_stamp(j) = now;
        PS.time_delay(j) = 0;
        PS.time_rise(j) = 0;
        PS.time_total(j) = 0;
                
        if (command >= xt_theta_c)
            % coarse
            sigma_good = xt_sigma_c;
            
        elseif (command<= xt_theta_f)
            % fine
            sigma_good = xt_sigma_f;
            
        else
            % blend
            sigma_good = max((command - xt_theta_f), 0.01) / max((xt_theta_c - xt_theta_f), 0.01);
            sigma_good = (sigma_good * (xt_sigma_c - xt_sigma_f)) + xt_sigma_f;
            
        end
        
        
        if (abs(change) <= 1.0)
            % Offset should be right near old position.
            tp_cor = muerr_old;
            
        elseif (abs(change) <= 2.0)
            % Little of each
            tp_cor = (muerr_old + ppval(PS.tp_coef, command)) / 2;
            
        else
            % Use the pre-evaluated coeficients.
            tp_cor = ppval(PS.tp_coef, command);

        end
        
        error_done = min(0.025 * abs(change), 0.50);
        
        if (command >= 32.0)
            % Coarse sensor region
            error_done = max(error_done, 0.10);
            
        else
            % Fine sensor region
            error_done = max(error_done, 0.05);
            
        end
        
        

        
    else
    % Continue
        k = k + 1;
        
    end
    
    if (i > (prelude + 1))
        % 1 loops after the ~200 loop prelude, we can calculate dx/dt, or else
        % initial data for this analysis will have odd first dx/dt in plot.
        rate_i = (tpencode - tpencode_old) ./ dt;
    else
        rate_i = 0;    
    end
        
    error_i = tpencode - command;
    
    PS.error_raw(i) = error_i;
    PS.rate_raw(i) = rate_i;
    
    if (mod(i,ctsm) == 0)
    % Data reduction to 5ms, for plot memory compacting
        m = m + 1;
        n = n + 1;    
    
        PS.rate_bar(m) =   ( 3 * PS.rate_raw(i) ...
                                + 2 * PS.rate_raw(i-1) ...
                                + 1 * PS.rate_raw(i-2)) ...
                                    / 6;
                                
        PS.error_bar(m) =  ( PS.error_raw(i) ...
                                + PS.error_raw(i-1)) ...
                                    / 2;
        
        PS.command_bar(m) = command;
        PS.change_bar(m) = change;
        PS.time_bar(m) = now;
        PS.tp_bar_1(m) = PS.tp_input_1(i);
        PS.tp_bar_2(m) = PS.tp_input_2(i);
        
        error_bar_grp(n) = PS.error_bar(m);
        rate_bar_grp(n) = PS.rate_bar(m);
    end
    
    % Need to push offset into each target; but commands far away have
    % different offsets, so roll out.
    error_cor = error_i - tp_cor;
    
    if (p < 0)
        
        % 'Adjust' only plots standard error points so don't use for anything else.
        PS.error_adj(m) = NaN;
        
        if (((now - stamp) >= xt_total_max) | (abs(error_cor) < error_done))
        % much too long of time, start StdDev
        % allow some calculation room for different changes
        % Resolution is decreased above 32d with handoffs
        % small changes must make small movement
        
             p = 0;
             PS.time_total(j) = max(PS.time_total(j), (PS.time_delay(j) + PS.time_rise(j)));
             PS.time_total(j) = min(PS.time_total(j), xt_total_max);
             
        elseif (abs(error_cor) <= abs(change * 0.10))
        % Within the last 10% of new postion.
            
            PS.time_total(j) = now - stamp;
            
        elseif (abs(tpencode - leave) <= abs(change * 0.10))
        % First 10% away from old position.
        
            PS.time_delay(j) = now - stamp;
            
        else
        % Rise Time Innner 80% of travel.
        
            PS.time_rise(j) = now - stamp - PS.time_delay(j);
                
        end    
        
    elseif ((now - stamp) > xt_total)
    % Standard deviation and binning of zones of command.
        p = p + 1;
        error_adj(p) = error_i;
        PS.error_adj(m) = error_i;
        
        c(x) = c(x) + 1;
        PS.error_adj_cat{x}(c(x)) = error_i;
        PS.command_adj_cat{x}(c(x)) = command;
    else
        % 'Adjust' only plots standard error points so don't use for anything else.
        PS.error_adj(m) = NaN;
    end
    
end

回复列表 (共4个回复)

沙发

matlab处理循环的效率太低,建议你采用混合编程

板凳

请教DingSun先生,何为混合编程?我也涉及多个for...end, while...end循环,效率较低,想提高程序运行的效率问题,请赐教!

3 楼

就是上段代码吗?


请明确那些是输入的参数,那些是要输出的参数


我看看,帮你写吧

4 楼

因为matlab处理循环的效率特低,可以使用c来处理处理,然后将结果返还到matlab中,


[quote]请教DingSun先生,何为混合编程?我也涉及多个for...end, while...end循环,效率较低,想提高程序运行的效率问题,请赐教!
[/quote]

我来回复

您尚未登录,请登录后再回复。点此登录或注册