samplefun
类: matlab.unittest.measurement.MeasurementResult
命名空间: matlab.unittest.measurement
对 MeasurementResult
数组样本应用函数
语法
[B1,...,Bm] = samplefun(fh,R)
[B1,...,Bm] = samplefun(fh,R,'UniformOutput',tf)
说明
[B1,...,Bm] = samplefun(
对 fh
,R
)MeasurementResult
数组的每个元素上的样本应用函数 fh
。samplefun
的每个输出参量对应于 fh
的输出参量,并与 R
具有相同的大小和形状。
[B1,...,Bm] = samplefun(
指示是否可以在不封装为元胞数组的情况下返回 fh
,R
,'UniformOutput',tf
)fh
的输出。默认情况下,fh
必须返回可以串联为数组的标量值。
输入参数
fh
— 要应用的函数
函数句柄
要对 MeasurementResult
数组的每个元素上的样本应用的函数,指定为函数句柄。
R
— 对测试套件运行测量试验得出的结果
MeasurementResult
数组
对测试套件运行测量试验得出的结果,指定为 MeasurementResult
数组。
tf
— 指示 fh
是否返回统一值的指示符
true
(默认) | false
指示 fh
是否返回统一值的指示符,指定为 true
或 false
。默认情况下,tf
为 true
,表示 fh
返回可以串联成数组的标量值。如果 tf
是 false
,fh
的输出可以具有不同的大小和数据类型。samplefun
以元胞数组形式返回这些非统一输出。
示例
对测量结果应用函数
在您的当前工作文件夹中创建一个基于类的测试,preallocationTest.m
,用于对不同的预分配方法进行比较。
classdef preallocationTest < matlab.perftest.TestCase methods(Test) function testOnes(testCase) x = ones(1,1e7); end function testIndexingWithVariable(testCase) id = 1:1e7; x(id) = 1; end function testIndexingOnLHS(testCase) x(1:1e7) = 1; end function testForLoop(testCase) for i=1:1e7 x(i) = 1; end end end end
创建一个测试套件。
suite = testsuite('preallocationTest');
构造一个可变计时试验,并运行测试。
import matlab.perftest.TimeExperiment
experiment = TimeExperiment.limitingSamplingError;
R = run(experiment,suite);
Running preallocationTest .......... .......... .......... ......Warning: Target Relative Margin of Error not met after running the MaxSamples for preallocationTest/testOnes. .... .......... .......... .......... .......... ..... Done preallocationTest __________
对于每个测试元素,求出样本的均值时间。
M = samplefun(@mean,R)
M = 0.0350 0.1351 0.0789 0.7337
对于每个测试元素,求出最小时间并进行最小时间索引。
[M,I] = samplefun(@min,R)
M = 0.0258 0.1169 0.0691 0.6531 I = 27 3 1 1
对测量结果应用自定义函数
在您的当前工作文件夹中创建一个基于类的测试,preallocationTest.m
,用于对不同的预分配方法进行比较。
classdef preallocationTest < matlab.perftest.TestCase methods(Test) function testOnes(testCase) x = ones(1,1e7); end function testIndexingWithVariable(testCase) id = 1:1e7; x(id) = 1; end function testIndexingOnLHS(testCase) x(1:1e7) = 1; end function testForLoop(testCase) for i=1:1e7 x(i) = 1; end end end end
创建一个测试套件。
suite = testsuite('preallocationTest');
使用 26 个样本测量值构造定次计时试验,并运行测试。
import matlab.perftest.TimeExperiment
experiment = TimeExperiment.withFixedSampleSize(26);
R = run(experiment,suite);
Running preallocationTest .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .... Done preallocationTest __________
在当前工作文件夹中,创建一个函数 customSampleFun
,以计算 26 个样本的均值,将均值转换为毫秒,并返回一个字符向量,指示均值时间是快还是慢。
function [mean_ms,speed] = customSampleFun(S) threshold_ms = 100; mean_ms = mean(S)*1e3; if mean_ms < threshold_ms speed = 'fast'; else speed = 'slow'; end end
对 MeasurementResult
数组中的每个元素应用 customSampleFun
。由于字符向量不是标量,因此请将 UniformOutput
指定为 false。
[mean_ms,speed] = samplefun(@customSampleFun,R,'UniformOutput',false)
mean_ms = 1×4 cell array [30.9500] [142.7037] [83.9830] [806.3446] speed = 1×4 cell array 'fast' 'slow' 'fast' 'slow'
版本历史记录
在 R2017a 中推出
另请参阅
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)