www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ApplyFunctionwithSingleOutputtoRowsExample.m

    %% Apply Function with Single Output to Rows  
% Apply the function |hypot| to each row of the 5-by-2 table |A| to find
% the shortest distance between the variables |x| and |y|.   
%
% Create a table, |A|, with two variables of numeric data. 
x = gallery('integerdata',10,[5,1],2);
y = gallery('integerdata',10,[5,1],8);

A = table(x,y)  

%% 
% Apply the function, |hypot|, to each row of |A|. The function |hypot| takes
% two inputs and returns one output. 
B = rowfun(@hypot,A,'OutputVariableNames','z') 

%%
% |B| is a table.  

%% 
% Append the function output, |B|, to the input table, |A|. 
[A B]