samplefun
Class: matlab.unittest.measurement.MeasurementResult
Namespace: 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(
applies
a function fh
,R
)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(
indicates
if the output of fh
,R
,'UniformOutput',tf
)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
fh
— Function to apply
function handle
Function to apply across the samples on each element of a MeasurementResult
array,
specified as a function handle.
R
— Results from running a measurement experiment on a test suite
MeasurementResult
array
Results from running a measurement experiment on a test suite,
specified as a MeasurementResult
array.
tf
— Indicator whether fh
returns uniform values
true
(default) | false
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
Apply Function to Measurement Results
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
Apply Custom Function to Measurement Results
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'
Version History
Introduced in R2017a
See Also
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.
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)