function varargout = make_mask_fatrices(K)
%
% function [GK,GKbar] = make_mask_fatrices(K)
%
% Create fatrix2 "fake matrices" for subsampling/embedding with mask K.
% Optionally also creates fatrix for complementary mask Kbar.
%
% Inputs:
%   K - the mask (logical)
%
% Outputs:
%   GK - fatrix for applying K
%   GKbar - (optional) fatrix for applying complement of K (Kbar)
%
% Copyright 2012-10-10, Daniel Weller, University of Michigan

arg.class = 'fatrix2';
N = numel(K);
M = sum(K(:));
arg.idim = [N,1];
arg.odim = [M,1];

if nargout >= 1
    arg.K = K(:);
    GK = fatrix2('arg',arg,'forw',@K_forw,'back',@K_back);
    varargout{1} = GK;
end

if nargout >= 2
    arg.K = ~arg.K;
    arg.odim = [N-M,1];
    GKbar = fatrix2('arg',arg,'forw',@K_forw,'back',@K_back);
    varargout{2} = GKbar;
end

end

%
% function y = K_forw(arg,x)
%
% Forward (subsampling) operator for mask K.
%
function y = K_forw(arg,x)

y = x(arg.K);
% y = y(:); % ensure vector

end

%
% function x = K_back(arg,y)
%
% Backward (embedding) operator for mask K.
%
function x = K_back(arg,y)

x = zeros(size(arg.K),class(y));
x(arg.K) = y;

end
