function out = putBinA(A,B,x,y)
% Function that places a small 2D matrix "B" into a larger one "A"
% with UL corner at coordinates (x,y). If the input args are given, then
% x is expected to be a 2 letter code for one of the nine
% standard locations in "A" (UL, UC, UR, ML, MC, MR, LL, LC, LR).
codes = {'UL', 'UC', 'UR', 'ML', 'MC', 'MR', 'LL', 'LC', 'LR'};
bx = size(B,1); by = size(B,2);
% check the input arguments are valid
if (nargin > 2) && (nargin < 5)
% check that A and B are 2D matricies
if (numel(size(A))~=2) || (numel(size(B))~=2)
error('The input matricies must be 2D arrays');
end
% check that A is >= B on both dims
if (size(A,1)<size(B,1)) || (size(A,2)<size(B,2))
error('The "A" matrix size must be >= the "B" matrix size');
end
if nargin == 3
coords = false;
y = 0;
% check that x is a 2 letter code
if size(strmatch(x,codes,'exact'))==[0 0]
error('The 2 letter code is incorrect');
end
else
% check that x and y are integers
if isnumeric(x) && isnumeric(y)
coords = true;
% check that the x,y placement lands B entirely contained in A
if (size(A,1) < (x+bx)) || (size(A,2) < (y+by))
error('The "B" matrix will fall outside of "A" with these coordinates');
end
end
end
else
error('Function putBinA expects either 3 or 4 input arguments');
end
out = A;
% Place B in A at (x,y) position
if coords
xx = x + bx - 1;
yy = y + by - 1;
out(x:xx,y:yy) = B;
else
% put B at the coded location
end