% 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 发布
该篇博客介绍了MATLAB的arrayfun函数,通过实例展示了如何计算结构数组中每个字段f1的元素数量、大小、列平均值,并进行结构间的数组比较。还提供了将十进制矩阵转换为二进制矩阵的方法。
摘要由CSDN通过智能技术生成