calc_average.m
来自「matlab programming for engineers(2nd)书籍源」· M 代码 · 共 35 行
M
35 行
function [ave, nvals] = calc_average(structure,field)
%CALC_AVERAGE Calculate the average of values in a field.
% Function CALC_AVERAGE calculates the average value
% of the elements in a particular field of a structure
% array. It returns the average value and (optionally)
% the number of items averaged.
% Define variables:
% arr -- Array of values to average
% ave -- Average of arr
% ii -- Index variable
%
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 03/04/07 S. J. Chapman Original code
%
% Check for a legal number of input arguments.
msg = nargchk(2,2,nargin);
error(msg);
% Create an array of values from the field
arr = [];
for ii = 1:length(structure)
arr = [arr structure(ii).(field)];
end
% Calculate average
ave = mean(arr);
% Return number of values averaged
if nargout == 2
nvals = length(arr);
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?