0

信号の極大値と極小値を見つけるアルゴリズムを作成しました。

[id_max, id_min] = find_max_min(signal);

ここで確認したいのは、最大値と最小値の変更が尊重されているかどうかです。

i.e. id_max(1)<id_min(1)<id_max(2)<id_min(2)<... 
we could start with a minimum..this is not known

仮定:

 id_max = [1 3 5 7 10 14 20];

 id_min = [2 4 6 8 16 19];

missing_max missing_min欠落している最大値と最小値の位置を示す2 つのベクトルが必要です。

2 つの連続する最小値 (最大値) の間に最大値 (最小値) がない場合、最大値 (最小値) の欠落が発生しid_min (id_max)ます。

この例では、id_min に 2 つの連続した値 (16 19) があり、間に最大値がないため、id_max の 7 番目の位置に最大値がありません。

次に、

missing_max = [7]

missing_min = [5]

以来

id_max = [1 3 5 7 10 14 X 20];

id_min = [2 4 6 8 X 16 19];(XI は欠損値をマーク)

交代が正しければ、ベクトルは空でなければなりません。for ループを使わずにそれを行う効率的な方法を提案できますか?

前もって感謝します

4

1 に答える 1

1

必要に応じて関数に適応できるスクリプトを次に示します。

    id_max = [1 3 5 7 10 14 20];
    id_min = [2 4 6 8 16 19];

    % Group all values, codify extremity (1-max, 0-min), and position
    id_all   = [          id_max,              id_min  ];
    code_all = [ones(size(id_max)), zeros(size(id_min))];
    posn_all = [  1:numel(id_max),     1:numel(id_min) ];

    % Reshuffle the codes and positions according to sorted IDs of min/max
    [~, ix]  = sort(id_all);
    code_all = code_all(ix);
    posn_all = posn_all(ix);

    % Find adjacent IDs that have the same code, i.e. code diff = 0
    code_diff = (diff(code_all)==0);

    % Get the indices of same-code neighbors, and their original positions
    ix_missing_min = find([code_diff,false] & (code_all==1));
    ix_missing_max = find([code_diff,false] & (code_all==0));

    missing_min    = posn_all(ix_missing_min+1);
    missing_max    = posn_all(ix_missing_max+1);

ID に関する注意事項:

  1. id_minandが行であることを確認してくださいid_max(空の場合でも)。
  2. それらの少なくとも 1 つが空でないことを確認してください。
  3. 並べ替える必要はありませんが、値は (ID 内および ID 間で) 一意である必要があります。

後で編集:

定義に関する新しい説明に基づく新しいバージョンのコード:

    id_max = [1 3 5 7 10 14 20];
    id_min = [2 4 6 8 16 19];
    %id_max = [12 14]
    %id_min = [2 4 6 8 10];

    id_min_ext = [-Inf, id_min];
    id_max_ext = [-Inf, id_max];

    % Group all values, and codify their extremity (1-max, 0-min), and position
    id_all   = [          id_max_ext,              id_min_ext  ];
    code_all = [ones(size(id_max_ext)), zeros(size(id_min_ext))];
    posn_all = [  0:numel(id_max),         0:numel(id_min)     ];

    % Reshuffle the codes and position according to sorted positions of min/max
    [~, ix] = sort(id_all);
    code_all = code_all(ix);
    posn_all = posn_all(ix);

    % Find adjacent IDs that have the same code, i.e. code diff = 0
    code_diff = (diff(code_all)==0);

    % Get the indices of same-code neighbours, and their original positions
    ix_missing_min = find([code_diff,false] & (code_all==1));
    ix_missing_max = find([code_diff,false] & (code_all==0));

    missing_min    = unique(posn_all(ix_missing_min-1))+1;
    missing_max    = unique(posn_all(ix_missing_max-1))+1;

ただし、コードには微妙なバグが含まれています。バグは、質問をした人、または求められていることが本当に明確になるように質問を改善した後、私によって削除されます。:-) 2 つの仮想極値 (ID = −∞ で 1 つの最大値と 1 つの最小値) があるという事実により、最初の欠落した極値が 2 回マークされる可能性があります: −∞ で 1 回、ID の最初の要素で 1 回リスト。unique()それを処理します(ただし、配列の最初の2つの要素が同じ値を持っているかどうかを確認するには、関数呼び出しが多すぎます)

于 2014-06-25T14:32:15.203 に答える