0

こんにちは私は、30秒間にわたって繰り返されるエクササイズの最大値と最小値のみを使用して、MatLabでマトリックスを作成する方法を見つけようとしています。

たとえば、データセットがある場合:

data = [1 3 5 7 9 6 4 2 3 6 8 10 7 6 4 2 1]

私が望んでいた結果は次のとおりです。

output = [1 9 2 10 1]

この関数は、絶えず変化する波形のピーク値のみをプロットします。

私が試したコードは次のとおりです。

size = length(data);    %Get the length of the dataset 
x = 1;                  %Set a counter value
maxplot = 0;            %Default, a maximum value has not yet been plotted

for x = 1:size-1
    a1 = data(1,x);     %Get two adjacent samples of the dataset
    a2 = data(1,x+1);

    v = 1;  %Set the initial column for the max points matrix

    while maxplot == 0
        if a1 > a2
            max(v,1) = a1;
            v = v + 1;
            maxplot = 1;
        end
    end

    if a1 < a2
        maxplot = 0;    
    end
end 

事前に返信してくださった方に感謝します。

ジャレド。

4

2 に答える 2

3

次のようなものを使用できます。

function Y = findpeaks(X)
    deltas = diff(X);
    signs = sign(deltas);
    Y = [true, (signs(1:(end-1)) + signs(2:end)) == 0, true];

findpeaks入力配列と同じ長さの論理配列を返しますX。マークされた値を抽出するには、論理配列でインデックスを付けるだけです。

例えば、

data = [1 3 5 7 9 6 4 2 3 6 8 10 7 6 4 2 1];
peaks = data(findpeaks(data))

出力する必要があります:

peaks =
    1    9    2   10    1

この関数は、入力配列で繰り返される値に対処するために特別なことは何もしません。それは読者の練習問題として残しておきます。

于 2012-04-16T02:12:26.973 に答える
2

このバージョンは John のものほどきれいではありませんが、平坦な部分があってもピークが失われません。

function peaks = findpeaks(data)
% Finds the peaks in the dataset

size = length(data);    %Get the length of the dataset 
x = 1;                  %Set a counter value
peaks = data(1,1);      %Always include first point

if size == 1  %Check for sanity
    return
end

lastdirection = 0;      %Direction of change

directions = sign( diff(data) ); %Directions of change
                                 % between neighboring elements

while x < size
    % Detect change in direction:
    if abs( directions(x) - lastdirection ) >= 2
        peaks = [peaks, data(1,x)];
        lastdirection = directions(x);
    else
        % This is here so that if lastdirection was 0 it would get
        % updated
        lastdirection = sign( lastdirection + directions(x) );
    end
    x = x+1;
end

peaks = [peaks, data(1,size)];
end
于 2012-04-16T03:09:05.490 に答える