% To run the examples in this section, create a nonscalar structure array with arrays of different sizes in field f1.
s(1).f1 = rand(3, 6);
s(2).f1 = magic(12);
s(3).f1 = ones(5, 10);
% Count the number of elements in each f1 field.
counts = arrayfun(@(x) numel(x.f1), s)
% The syntax @(x) creates an anonymous function. This code returns%
% counts =
% 18 144 50
% Compute the size of each array in the f1 fields.
[nrows, ncols] = arrayfun(@(x) size(x.f1), s)
% This code returns
%
% nrows =
% 3 12 5
% ncols =
% 6 12 10
% Compute the mean of each column in the f1 fields of s. Because the output is nonscalar, set UniformOutput to false.
averages = arrayfun(@(x) mean(x.f1), s, 'UniformOutput', false)
% This code returns
%
% averages =
% [1x6 double] [1x12 double] [1x10 double]
% Create additional nonscalar structures t and u, and test for equali
matlab arrayfun 函数使用
最新推荐文章于 2024-07-24 22:33:18 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)