2012-10-19 3 views
0

저는 교수님을 위해 제 RA 업무를 거의 마쳤습니다. 그러나, 나는 프로그래밍 문제를 가지고있다. 교수가 저에게 요구 한 구현의 마지막 단계는 행렬에 대해 쓴 모든 함수를 실행해야한다는 것입니다. 어떤 일이 일어날 것은 그 행렬의 각 열에 대해, 내가 예를 들어, 다음과 같습니다 내가 쓴 함수를 호출하는 것입니다 : 이제행렬의 각 열에 대해 함수를 실행하도록 MATLAB에 요청하는 방법은 무엇입니까?

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 개의 사용자 작성 함수가 있으며 각 함수에 대해 5000 x 10000 행렬의 각 열을 실행해야합니다.

내 친구가 내 행렬의 n 번째 열을 가져 오는 함수를 작성하고 그 결과 벡터 내 행렬의 각 기능을 실행, 벡터로서 돌려 제안했다. 다른 아이디어? 솔직히 말해서, 나는 그가 제안한 그 기능을 어떻게 쓰는지 모른다. 감사!

+0

당신은, 벡터 사소한 같이 행렬의 N 번째 열을 반환 MTX 친구의 조언을 따르십시오. 그런 다음 각 사용자 정의 함수에 feval()을 사용하십시오. – engineerC

+0

내가 질문에 대답했다고 생각되면 내 대답 옆의 체크 표시를 클릭하여 답을 표시하십시오. 그렇지 않다면 왜 내 대답을 개선 할 수 있는지 알려주십시오. 건배. –

+0

안녕 콜린, 난, 난 당신이 내 질문에 대답했을 수도 죄송 생각 해요,하지만 난 내 컴퓨터가 느리기 때문에 결과를 볼 못하고있다. –

답변

3

출력은 서로 다른 크기의 벡터의 경우 그 배열은 가로 (세로) 적층 출력 벡터를 하나의 거대한 길이 열 (컬럼) 벡터가 아닌 경우, 다음 하나 개의 숫자 어레이 모두를 저장할 수 없다.

따라서 I 출력을 저장하는 셀 어레이를 사용하여 제시한다. 피크에는 셀 배열의 첫 번째 행을 사용하고 골짜기에는 두 번째 행을 사용하십시오. 예를 들어 :

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 

공지 사항 I 지수 셀 어레이는 중괄호 대신 (){}를 사용하는 것이다. 이 herehere에 대해 읽어보십시오.

여기가로 출력을 구조화, 당신은 당신이 당신의 매트릭스를 통해 실행하는 각 기능에 대한 하나 개의 셀 어레이가 필요합니다. 아마도 응용 프로그램 (기술 분석)을 고려할 때 이것이 가장 합리적이라고 생각합니다. (: N)

+0

안녕하세요 콜린,이 대답에 대한 하나의 질문. 실제로 그것은 좋은 것 같지만, 예를 들어, 대부분의 내 사용자 정의 함수의 출력 결과는 열 벡터입니다. 위의 코드 구문을 작성하는 방식에 문제가 있습니까? 아니면 내 출력에 사용되는 벡터 유형에 관계없이이 작업을 수행 할 수 있습니다. 감사! 나는 이것이 좋은 대답이라고 생각하지만, 아직 결과가 없습니다. 내 컴퓨터가 천천히 놀랐어. :) –

+0

@JulioGalvez 예, CurPeaks 및 CurTroughs는 열 벡터가 될 수 있습니다. 셀 배열의 단일 셀은 숫자 스칼라/벡터/행렬, 구조체, 문자열 또는 심지어 다른 셀 배열을 저장할 수 있습니다. 그들은 매우 유연한 저장 솔루션이며 다른 길이의 출력 벡터를 가지고 있으므로 현재 상황에서 유용합니다. 따라서 하나의 직사각형 숫자 행렬에 모두 저장 될 수는 없습니다. –

+0

예를 들어 CurPeaks와 CurTroughs와 같은 다음 함수가 출력에 의존하면 코드를 작성할 때 본질적으로 여기에 표시된 것과 동일한 형식을 갖지만 구문은 다음과 같습니다 : '에 대한 m = 1 : '% 헤드 및 'cadbhead1 = headshoulders Patterns' 어깨 ncol' (cadboot (:, m), cadpeaktrough1 {1, m}, {2 cadpeaktrough1, m} horizsym); ' '% Store 결과 벡터.' 'cadheadshoulder1 {1, m} = cadbhead1;' 'end' –

관련 문제