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: MRcart2im.m
    path of file: ./repos/GIBBON/lib
    
    the code of the file until where you have to start completion: function [I,J,K]=MRcart2im(Xc,Yc,Zc,v,OR,r,c)

%--------------------------------------------------------------------------
% function [I,J,K]=MRcart2im(X,Y,Z,v,OR,r,c)
% 
% This function calculates the image coordinates I,J,K of the cartesian 
% coordinates X,Y,Z based on the voxel dimensions v, the origin OR (cartesian
% coorindates of the point I=1, J=1, K=1), the offsets r anc c.
%
% v=N_info.PixelSpacing;
% v(3)=N_info.SliceThickness;
% OR=N_info.ImagePositionPatient;
% r=N_info.ImageOrientationPatient(4:6);

    
function [I,J,K]=MRcart2im(Xc,Yc,Zc,v,OR,r,c)

%--------------------------------------------------------------------------
% function [I,J,K]=MRcart2im(X,Y,Z,v,OR,r,c)
% 
% This function calculates the image coordinates I,J,K of the cartesian 
% coordinates X,Y,Z based on the voxel dimensions v, the origin OR (cartesian
% coorindates of the point I=1, J=1, K=1), the offsets r anc c.
%
% v=N_info.PixelSpacing;
% v(3)=N_info.SliceThickness;
% OR=N_info.ImagePositionPatient;
% r=N_info.ImageOrientationPatient(4:6);
% c=N_info.ImageOrientationPatient(1:3);
%
% Based on:
% http://cmic.cs.ucl.ac.uk/fileadmin/cmic/Documents/DavidAtkinson/DICOM_6up.pdf
% and the file "TranTransform matrix between two dicom image coordinates"
% from the matlab central file exchange written by Alper Yaman. 
%-------------------------------------------------------------------------- 

reForm=~isvector(Xc);

%Switch convention
X=Yc; Y=Xc; Z=Zc;

size_M=size(Xc);

% r => row direction vector
r=vecnormalize(r);

% c => column direction vector
c=vecnormalize(c);

%Determine s => slice direction vector N.B. defined as cross(c,r) not
%cross(r,c) 
s=cross(c',r'); %Determine s => slice direction vector
s=vecnormalize(s);

X=X(:)';Y=Y(:)';Z=Z(:)';

%Translation
T  = [1 0 0 OR(1);...
      0 1 0 OR(2);...
      0 0 1 OR(3);...
      0 0 0 1]; 
%Rotation
R  = [r(1) c(1) s(1) 0;...
      r(2) c(2) s(2) 0;...
      r(3) c(3) s(3) 0;...
      0    0    0    1];
%Scaling  
S  = [v(1) 0    0    0;...
      0    v(2) 0    0;...
      0    0    v(3) 0;...
      0    0    0    1];
  
T0 = eye(size(T));

M  = T * R * S * T0; %The transformation matrix

XYZ=ones(4,length(X));
XYZ(1,:)=X;
XYZ(2,:)=Y;
XYZ(3,:)=Z;

IJK=(M\XYZ)';

I=IJK(:,1)+1; J=IJK(:,2)+1; K=IJK(:,3)+1;

if reForm %If the input is not a vector reshape
    I=reshape(I,size_M);
    J=reshape(J,size_M);
    K=reshape(K,size_M);
end