You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: exchange.m
    path of file: ./repos/opt_matlab/第12章/例12.1
    
    the code of the file until where you have to start completion: function [df,route_after]=exchange(route,d,flag,type)

    
function [df,route_after]=exchange(route,d,flag,type)
n=length(route);
switch type
    case 1   %交换1,2个城市
        loc=sort(ceil(rand(1,2)*n));
        if flag==1    %固定起点
            while loc(1)==loc(2)||loc(1)==1
               loc=sort(ceil(rand(1,2)*n));
            end