2012-04-16 2 views
0

안녕하세요, 저는 30 초 동안 반복되는 운동의 최대 값과 최소값 만 사용하여 MatLab에서 행렬을 만드는 방법을 찾으려고합니다. 예를 들어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 

하라구요

자레드.

+0

그냥이 작업을 수행하는 함수를 작성 봤어? 그것은 열심히 보이지 않습니다 ... – trutheality

+0

나는 시도했지만 MatLab을 처음 사용했습니다. 나는 MatLab이 'Busy'로 붙어있어서 우연히 무한 루프를 만들었다 고 생각한다. – jazibobs

+0

당신이 시도한 것을 게시 할 수 있고 다른 누군가가 당신을 도울 수있다 ... – trutheality

답변

2

이 같은 것을 사용할 수 있습니다

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 

이 함수는 입력 값이 반복 배열에 대처하는 특별 아무것도하지 않는다. 저는 이것을 독자를위한 운동으로 남겨 둡니다.

+0

피크에 평평한 부분이있을 때 (즉' ... 8 10 10 7 ...). – trutheality

+0

@trutheality : True. 메모를 추가하겠습니다. –

+0

감사합니다 이것은 절대적으로 완벽합니다. 나는 "findpeaks"에서 누락 된 쉬운 옵션이 있다는 것을 전혀 몰랐습니다. – jazibobs

2

이 버전은 요한만큼 예쁜 아니지만, 평평한 부분이있는 경우는 피크를 잃지 않는다 :

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 
관련 문제