0

教授のRAの仕事はほぼ終わりました。しかし、私は私を困惑させているプログラミングの質問があります。教授から依頼された実装の最後のステップは、行列用に作成したすべての関数を実行する必要があることです。何が起こるかというと、その行列の各列について、私が書いた関数を呼び出します。たとえば、次のようになります。

function [xpeaks, xtroughs]=peaktrough(x,cutoff)

% This function is a modified version of the algorithm used to identify
% peaks and troughs in a series of prices. This will be used to identify
% the head and shoulders algorithm. The function gives you two vectors:
% PEAKS - an indicator vector that identifies the peaks in the function,
% and TROUGHS - an indicator vector that identifies the troughs of the
% function, and a MATRIX - ptdata, which is a matrix of the location and
% the value of the peak/trough.

% The input is the vector of exchange rate series, and the cutoff
% used for the selection of the final peaks and troughs.

% Finding all possible peaks and troughs of our vector.
[posspeak,possploc]=findpeaks(x);
[posstrough,posstloc]=findpeaks(-x);
posspeak=posspeak';
posstrough=posstrough';

% Initialize vector of peaks and troughs.
numobs=length(x);
prelimpeaks=zeros(numobs,1); 
prelimtroughs=zeros(numobs,1);
numpeaks=numel(possploc);
numtroughs=numel(posstloc);

% Indicator for possible peaks and troughs.
for i=1:numobs
    for j=1:numpeaks
        if i==possploc(j);
            prelimpeaks(i)=1;
        end
    end
end

for i=1:numobs
    for j=1:numtroughs
        if i==posstloc(j);
            prelimtroughs(i)=1;
        end
    end
end

% Vector that gives location.
location=1:1:numobs;
location=location';

% From the list of possible peaks and troughs, find the peaks and troughs
% that fit Chang and Osler [1999] definition.
% "A peak is a local minimum at least x percent higher than the preceding
% trough, and a trough is a local minimum at least x percent lower than the
% preceding peak." [Chang and Osler, p.640]

peakcutoff=1.0+cutoff; % cutoff for peaks
troughcutoff=1.0-cutoff; % cutoff for troughs

% First peak and first trough are initialized as previous peaks/troughs.

prevpeakloc=possploc(1);
prevtroughloc=posstloc(1);

% Initialize vectors of final peaks and troughs.
vectpeak=zeros(numobs,1);
vecttrough=zeros(numobs,1);

% We first check whether we start looking for peaks and troughs.
for i=1:numobs
    if prelimpeaks(i)==1;
       if i>prevtroughloc;
           ratio=x(i)/x(prevtroughloc);
           if ratio>peakcutoff;
               vectpeak(i)=1;
               prevpeakloc=location(i);
           else vectpeak(i)=0;
           end
       end
    elseif prelimtroughs(i)==1;
        if i>prevpeakloc;
            ratio=x(i)/x(prevpeakloc);
            if ratio<troughcutoff;
                 vecttrough(i)=1;
                 prevtroughloc=location(i);
            else vecttrough(i)=0;
            end
        end
    else
        vectpeak(i)=0;
        vecttrough(i)=0;
    end
end

% Now from the final peaks and troughs, we now write the matrix of the
% peaks and troughs of the exchange rate series.
xpeaks=find(vectpeak);
xtroughs=find(vecttrough);
end

ただし、列が異なれば、サイズの異なるベクトルになってしまいます。私が知りたいのは、どうすればいいのかということです。20個のユーザー作成関数があり、関数ごとに、5000x10000マトリックスの各列を実行する必要があります。

私の友人は、行列のn番目の列を取得し、それをベクトルとして返す関数を作成し、その結果のベクトルに対して行列の各関数を実行することを提案しました。他のアイデアはありますか?実は、正直なところ、彼が提案した関数の書き方がわかりません。ありがとう!

4

1 に答える 1

3

出力が異なるサイズのベクトルである場合、その配列が水平方向(垂直方向)にスタックされた出力ベクトルを持つ1つの巨大な長い行(列)ベクトルでない限り、それらすべてを1つの数値配列に格納することはできません。

したがって、セル配列を使用して出力を格納することをお勧めします。セル配列の最初の行をピークに使用し、2番目の行を谷に使用します。例えば:

NumCol = 10000
YourMatrix = randn(5000, NumCol);
YourCellArrayOutput = cell(2, NumCol);
for m = 1:NumCol
    [CurPeaks, CurTroughs] = peaktrough(YourMatrix(:, m), cutoff);
    YourCellArrayOutput{1, m} = CurPeaks;
    YourCellArrayOutput{2, m} = CurTroughs;
end

{}の代わりに中括弧を使用してセル配列にインデックスを付けることに注意してください()ここここでこれについて読んでください。

ここにあるように出力を構造化すると、マトリックス上で実行する関数ごとに1つのセル配列が必要になります。アプリケーション(テクニカル分析)を考えると、おそらくそれが最も理にかなっていると思います。

于 2012-10-19T01:27:30.297 に答える