Main Content

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 数组的每个元素上的样本应用函数 fhsamplefun 的每个输出参量对应于 fh 的输出参量,并与 R 具有相同的大小和形状。

[B1,...,Bm] = samplefun(fh,R,'UniformOutput',tf) 指示是否可以在不封装为元胞数组的情况下返回 fh 的输出。默认情况下,fh 必须返回可以串联为数组的标量值。

输入参数

全部展开

要对 MeasurementResult 数组的每个元素上的样本应用的函数,指定为函数句柄。

对测试套件运行测量试验得出的结果,指定为 MeasurementResult 数组。

指示 fh 是否返回统一值的指示符,指定为 truefalse。默认情况下,tftrue,表示 fh 返回可以串联成数组的标量值。如果 tffalsefh 的输出可以具有不同的大小和数据类型。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 中推出

另请参阅