samplefun
类: matlab.unittest.measurement.MeasurementResult
命名空间: matlab.unittest.measurement
对 MeasurementResult
数组样本应用函数
语法
[B1,...,Bm] = samplefun(fh,R)
[B1,...,Bm] = samplefun(fh,R,'UniformOutput',tf)
说明
输入参数
要对 MeasurementResult
数组的每个元素上的样本应用的函数,指定为函数句柄。
对测试套件运行测量试验得出的结果,指定为 MeasurementResult
数组。
指示 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 Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)