function output = wrap_matrix(data,vmin,vmax)

%%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%Wrap the input matrix into [vmin,vmax]
%%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

index=find(isnan(data));
min_index       = find(data <= vmin) ;   
output          = data;
%
while isempty(min_index) ~= 1 
    output(min_index)  = output(min_index) + (vmax-vmin);   
    min_index          = find(output <= vmin) ;       
end   
%
max_index = find(data > vmax) ;                     
while isempty(max_index)~= 1   
    output(max_index)  = output(max_index) - (vmax-vmin);   
    max_index          = find(output > vmax) ;            
end
%
output(index) = nan;
%
