0
votes

I'm nearly finished with my RA job for my professor; however, I have a programming question that is stumping me. The last step in the implementation that my professor asked me to do is that I need to run all the functions that I wrote for a matrix. What is going to happen is that for each column of that matrix, I will call a function that I wrote, which for example, looks like this:

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

Now, the thing is, however, for different columns, I will end up with vectors of different sizes. What I want to know is, how do I go about it? I have 20 user-created functions, and for each function, I need to run each of the columns in the 5000 x 10000 matrix.

A friend of mine suggested that I write a function that gets the nth column of my matrix, and then returns it as a vector, then run each of the functions of my matrix for that resulting vector. Any other ideas? Actually, to be honest, I do not know how to write that function that he suggested. Thanks!

1
If you follow your friends advice, returning the Nth column of a matrix as a vector is trivial, mtx(:,N). Then use feval() with each of your custom functions.engineerC
If you feel I have answered the question, then please mark the question answered by clicking the tick mark next to my answer. If not, then please let me know why so I can attempt to improve my answer. Cheers.Colin T Bowers
Hi Colin, I'm sorry, I think you might have answered my question, but I have yet to see the results because my computer is slow.Julio Galvez

1 Answers

3
votes

If the output is vectors of different sizes, then you can't store them all in one numeric array unless that array is one huge long row (column) vector with output vectors stacked horizontally (vertically).

Therefore I would suggest using a cell array to store the output. Use the first row of the cell array for peaks, and the second row for troughs. For example:

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

Notice that I index the cell array using curly braces {} instead of (). Read about this here and here.

Structuring your output as I have here, you will need one cell array for each function that you run over your matrix. I think that probably makes the most sense given the application (technical analysis).