Main Content

samplefun

Class: matlab.unittest.measurement.MeasurementResult
Package: matlab.unittest.measurement

Apply function across samples of MeasurementResult array

Syntax

[B1,...,Bm] = samplefun(fh,R)
[B1,...,Bm] = samplefun(fh,R,'UniformOutput',tf)

Description

[B1,...,Bm] = samplefun(fh,R) applies a function fh across the samples on each element of a MeasurementResult array. Each output argument from samplefun corresponds to an output argument from fh and has the same size and shape as R.

[B1,...,Bm] = samplefun(fh,R,'UniformOutput',tf) indicates if the output of fh can be returned without encapsulation in a cell array. By default, fh must return scalar values that can be concatenated into an array.

Input Arguments

expand all

Function to apply across the samples on each element of a MeasurementResult array, specified as a function handle.

Results from running a measurement experiment on a test suite, specified as a MeasurementResult array.

Indicator whether fh returns uniform values, specified as true or false. tf is true by default, which indicates that fh returns scalar values that can be concatenated into an array. If tf is false, the outputs of fh can have different sizes and data types. samplefun returns these nonuniform outputs in a cell array.

Examples

expand all

In your current working folder, create a class-based test, preallocationTest.m, that compares different methods of preallocation.

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

Create a test suite.

suite = testsuite('preallocationTest');

Construct a variable time experiment, and run the tests.

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
__________

For each test element, find the mean time of the samples.

M = samplefun(@mean,R)
M =

    0.0350    0.1351    0.0789    0.7337

For each test element, find the minimum time and index to the minimum time.

[M,I] = samplefun(@min,R)
M =

    0.0258    0.1169    0.0691    0.6531


I =

    27     3     1     1

In your current working folder, create a class-based test, preallocationTest.m, that compares different methods of preallocation.

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

Create a test suite.

suite = testsuite('preallocationTest');

Construct a fixed time experiment with 26 sample measurements, and run the tests.

import matlab.perftest.TimeExperiment
experiment = TimeExperiment.withFixedSampleSize(26);
R = run(experiment,suite);
Running preallocationTest
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
....
Done preallocationTest
__________

In your current working folder, create a function, customSampleFun, that computes the mean of each of the 26 samples, converts the mean to milliseconds, and returns a character vector indicating if the mean time was fast or slow.

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

Apply customSampleFun to each element in the MeasurementResult array. Since the character vectors aren't scalar, specify UniformOutput as 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'

Introduced in R2017a