【问题标题】:Maximum and minimum points of a dataset in MatLabMatLab中数据集的最大和最小点
【发布时间】:2012-04-16 02:02:38
【问题描述】:

您好,我正在尝试找到一种方法在 MatLab 中创建一个矩阵,其中仅包含在 30 秒内重复的练习的最大值和最小值。

例如,如果我有数据集:

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 

感谢提前回复的人,

贾里德。

【问题讨论】:

  • 你有没有试过写一个这样的函数?看起来没那么难……
  • 我已经尝试过,但我是使用 MatLab 的新手。我想我不小心创建了一个无限循环,因为 MatLab 卡在“忙碌”状态
  • 您可以发布您尝试过的内容,然后有人可以帮助您...
  • 是的,如果 a1 &lt;= a2... 看起来像 someone has written a function that does this,则您的 while 外观永远不会退出。

标签: matlab max data-analysis


【解决方案1】:

你可以这样使用:

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

这个函数没有做任何特殊的事情来处理输入数组中的重复值。我把它留给读者作为练习。

【讨论】:

  • 在峰顶有平坦部分时似乎失败了(即... 8 10 10 7 ...)。
  • @trutheality:是的。我会添加注释。
  • 谢谢,这绝对是完美的。我不知道我错过了“findpeaks”中有这么简单的选项。
【解决方案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

【讨论】:

    猜你喜欢
    • 2013-06-05
    • 2014-06-21
    • 2011-01-20
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    相关资源
    最近更新 更多