メインコンテンツ

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

samplefun

R2026a

クラス: 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) は、関数 fh を配列 MeasurementResult の各要素のサンプルにまたがって適用します。samplefun からの各出力引数は、fh の出力引数に対応していて、サイズと形状は R と同じです。

[B1,...,Bm] = samplefun(fh,R,'UniformOutput',tf) は、cell 配列でのカプセル化を行わずに fh の出力を返せるかどうかを示します。既定では、fh は連結して配列にすることができるスカラー値を返さなければなりません。

入力引数

すべて展開する

配列 MeasurementResult の各要素のサンプルにまたがって適用する関数。関数ハンドルとして指定します。

テスト スイートで測定実験を実行した結果。配列 MeasurementResult として指定します。

fh が一様な値を返すかどうかのインジケーター。true または false として指定します。tf の既定値は true であり、これは fh が、連結して配列にできるスカラー値を返すことを示します。tf が false の場合、fh の出力は異なるサイズとデータ型をもつことができます。samplefun は、これらの一様でない出力を cell 配列に出力します。

例

すべて展開する

現在の作業フォルダーに、さまざまな事前割り当てメソッドを比較するクラスベースのテスト 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

customSampleFun を配列 MeasurementResult の各要素に適用します。文字ベクトルはスカラーではないため、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 で導入