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: ShapeRecognition.m
    path of file: ./repos/shape-context-ocr/Matlab OCR/Scripts
    
    the code of the file until where you have to start completion: %% RECOGNITION STAGE
    % Steps for detecting 
    
function [out, cost] = affineTransform2 (A, B)
    % Add column of ones acting as homogeneous coordinates
    A = [ones(size(A,1),1) A];
    B = [ones(size(B,1),1) B];
    
    % Calculate affine matrix
    %[R,t] = rigid_transform_3D(A, B);
    A = A + repmat(mean(B) - mean(A), length(A), 1);
    R = (pinv(A)*B)';
    t = -R*mean(A)' + mean(B)';
    
    % Calculate bend