matrixfunction

by

 

28 Feb 2009 (Updated )

summary

matrixfunt(hFunction,A,B,Dim)
function Y = matrixfunt(hFunction,A,B,Dim)
% matrixfunt
%   Y = matrixfunt(hFunction,A,B,Dim)
%
%   Invokes hFunction on the columns of matrix A and rows of matrix B. 
%   hFunction is a function handle that accepts two vectors.
%   hFunction is invoked aCols*bRows times.
%   Y is a cell matrix of size([aCols bRows]). 
%   If Dim is 2, hFunction operates on the rows of matrix A and the columns of matrix B.
%
%   Example
%       A = [1 1 1; 2 2 2; 3 3 3]
%       Y = matrixfunt(@(X,Y)sum(X-Y),A,A);         
%       Y = [3]    [0]    [-3]
%           [3]    [0]    [-3]
%           [3]    [0]    [-3]
%
%       Y = matrixfunt(@(X,Y)sum(X-Y),A,A,2); 
%       Y = [-3]    [-3]    [-3]
%           [ 0]    [ 0]    [ 0]
%           [ 3]    [ 3]    [ 3]
%
%   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 column 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 row of B
            Y{i,j} = hFunction(A(:,i),B(j,:).');	% Invoke function
        end
    end
end
end
   

Contact us