matrixfunction

by

 

28 Feb 2009 (Updated )

summary

matrixfuni(hFunction,A,Dim)
function Y = matrixfuni(hFunction,A,Dim)
% matrixfuni
%   Y = matrixfuni(hFunction,A,B,Dim)
%
%   Invokes hFunction on the columns of matrix A. 
%   hFunction is a function handle that accepts a vector.
%   hFunction is invoked aCols times.
%   Y is a cell matrix of size([aCols 1]). 
%   If Dim is 2, hFunction operates on the rows of matrices A and B.
%
%   Example
%       A = magic(3);
%       Y = matrixfuni(@sum,A);         
%       Y = [15]
%           [15]
%           [15]
%
%       Y = matrixfuni(@sum,A,2);
%       Y = [15]
%           [15]
%           [15]
%
%   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<3 || isempty(Dim), Dim = 1; end

%%
nA = size(A.',Dim);              	% Matrix size
Y = cell([nA 1]);                   % Predefine loop variable
if Dim==2
    for i = 1:nA                    % Loop through each row of A
        Y{i} = hFunction(A(i,:));   % Invoke function
    end
elseif Dim==1
    for i = 1:nA                    % Loop through each column of A
    	Y{i} = hFunction(A(:,i));   % Invoke function
    end
end
end

Contact us