%  ConsMatRight  Reconstructs a matrix using rotational patterns
%
%    A=ConsMatRight(Grot,Gind,R) Constructs the matrix A=RQ, where the unitary
%    matrix Q is represented by the factorization Grot, Gind.
%    R is an upper triangular matrix, on which the rotations will be applied.
%
%
%    Software written by Raf Vandebril
%    raf.vandebril-at-cs.kuleuven.be
%    Revision Date: September 1, 2010
%

function R=ConsMatRight(Grot,Gind,R)

  % Check if the input is not empty
    if ~isempty(Gind)
  
      % By default we assume that the matrix Gind is not ordered,
      % Most likely this matrix will be ordered, but for consistency 
      % we first order the rotations in the order that they can be performed
      [SG,I]=sort(Gind(:,3));
      Gind=Gind(I,:); % Rorder the index storage
      Grot=Grot(:,I); % Rorder the rotations themselves
      
      for i=1:size(Gind,1)
        % Assign the rotation and execute it
        Giv=[Grot(1,i) Grot(2,i) ; -Grot(2,i)' Grot(1,i)'];
        R(:,[Gind(i,1),Gind(i,2)])=R(:,[Gind(i,1),Gind(i,2)])*Giv;
      end
    end
    
    
    
