function Y = matrixfun(hFunction,A,B,Dim)
% matrixfun
% Y = matrixfun(hFunction,A,B,Dim)
%
% Invokes hFunction on the columns of matrices A and B.
% hFunction is a function handle that accepts two vectors.
% hFunction is invoked aCols*bCols times.
% Y is a cell matrix of size([aCols bCols]).
% If Dim is 2, hFunction operates on the rows of matrices A and B.
%
% Example
% A = [1 1 1; 2 2 2; 3 3 3]
% Y = matrixfun(@(X,Y)sum(X-Y),A,A);
% Y = [0] [0] [0]
% [0] [0] [0]
% [0] [0] [0]
%
% Y = matrixfun(@(X,Y)sum(X-Y),A,A,2); % = matrixfun(@(X,Y)sum(X-Y),A.',A.')
% Y = [0] [-3] [-6]
% [3] [ 0] [-3]
% [6] [ 3] [ 0]
%
% See also
% matrixfun matrixfunrr matrixfunrc matrixfuncc matrixfuncr matrixfunt
% matrixfunn matrixfunrrn matrixfunrcn matrixfunccn matrixfuncrn matrixfuntn
% matrixfuni matrixfunir matrixfunic
%
%% Author Information
% Pierce Brady
% Smart Systems Integration Group
% Cork Institute of Technology
%
%% Assign defaults
if nargin<4 || isempty(Dim), Dim = 1; end
%%
nA = size(A.',Dim); % Matrix size
nB = size(B.',Dim); % Matrix size
Y = cell([nA nB]); % Predefine loop variable
if Dim==2
for i = 1:nA % Loop through each row of A
for j = 1:nB % Loop through each row of B
Y{i,j} = hFunction(A(i,:),B(j,:)); % Invoke function
end
end
elseif Dim==1
for i = 1:nA % Loop through each column of A
for j = 1:nB % Loop through each column of B
Y{i,j} = hFunction(A(:,i),B(:,j)); % Invoke function
end
end
end
end